我有一个模板如下:
<div class="row search-panel">
<div ng-show="loadingAirports">
<i class="glyphicon glyphicon-refresh"></i> Searching...
</div>
<div ng-show="noResults">
<i class="glyphicon glyphicon-remove"></i> No Results Found
</div>
<div class="col-md-6">
<pre>Source: {{srcAirport | json}}</pre>
<p class="input-group">
<input type="text" class="form-control" ng-model="srcAirport" uib-typeahead="airport as airport.name for airport in findAirports($viewValue)"
typeahead-min-length="3" typeahead-loading="loadingAirports" typeahead-no-results="noResults">
</p>
</div>
<div class="col-md-6">
<pre>Destination: {{destAirport | json}}</pre>
<p class="input-group">
<input type="text" class="form-control" ng-model="destAirport" uib-typeahead="airport as airport.name for airport in findAirports($viewValue)"
typeahead-min-length="3" typeahead-loading="loadingAirports" typeahead-no-results="noResults">
</p>
</div>
</div>
还有一个使用上述模板的自定义指令:
(function() {
var app = angular.module('airport-search', ['ui.bootstrap']);
app.directive('airportSearch', function() {
var airportSearchCtrl = ['$scope',
function($scope) {
$scope.airports = {
"data": [{
"name": "Le Touquet Paris Plage",
"city": "Le Tourquet",
"country": "France",
"faa_code": "LTQ",
"icao": "LFAT",
"time_zone_offset": "+01:00"
}, {
"name": "Sparrevohn Lrrs",
"city": "Sparrevohn",
"country": "United States",
"faa_code": "SVW",
"icao": "PASV",
"time_zone_offset": "-09:00"
}, {
"name": "Palm Beach Co Park",
"city": "West Palm Beach",
"country": "United States",
"faa_code": "LNA",
"icao": "KLNA",
"time_zone_offset": "-05:00"
}, {
"name": "Scappoose Industrial Airpark",
"city": "San Luis",
"country": "United States",
"faa_code": "SPB",
"icao": "KSPB",
"time_zone_offset": "-08:00"
}, {
"name": "Sheppard Afb Wichita Falls Muni",
"city": "Wichita Falls",
"country": "United States",
"faa_code": "SPS",
"icao": "KSPS",
"time_zone_offset": "-06:00"
}]
};
$scope.findAirports = function(val) {
return new Promise(function(resolve, reject) {
resolve($scope.airports);
}).then(function(searchResults) {
return searchResults.data.map(function(airport) {
return airport;
});
});
};
}
];
return {
restrict: 'E',
templateUrl: 'airport-search.html',
scope: {
srcAirport: '=',
destAirport: '='
},
controller: airportSearchCtrl
};
});
})();
这可行,但正如您所见,模板重复了文本字段的属性,但模型除外。能够使用ng-repeat
然后改变模型会很好。我试过了,ng-repeat="model in [srcAirport, destAirport] track by $index"
但没有编译。这可以做到吗?
这是一个现场Plunker 示例。