【发布时间】:2019-02-12 16:36:50
【问题描述】:
下面我正在使用我的自定义指令type-ahead-custom一个接一个。
第一次type-ahead-custom="maps"
第二次type-ahead-custom="maps1"
如果我在指令中访问属性type-ahead-custom 值,它总是返回“maps1”。我该如何解决这个问题?
这里是 plunker:https://plnkr.co/edit/hXlNJKboSlA2lAvRqYF1
<form class="form-horizontal" ng-controller="myCtrl" >
<div class="form-group">
<div>
<label for="account" class="col-sm-2 col-md-2 control-label customize-label ">Typeahead 1</label>
<div class="col-sm-8">
<div class="inner-addon right-addon">
<input type="text" ng-model="selectedOptions.planes" uib-typeahead="plane as plane.formatted_address for plane in search($viewValue)" type-ahead-custom="maps" typeahead-loading="loadingdata" typeahead-no-results="noResults" class="form-control ng-valid ng-dirty ng-valid-parse ng-touched" aria-autocomplete="list" aria-expanded="false" aria-owns="typeahead-4-8758" />
</div>
</div>
</div>
</div>
<div class="form-group">
<div>
<label for="account" class="col-sm-2 col-md-2 control-label customize-label ">Typeahead 2</label>
<div class="col-sm-8">
<div class="inner-addon right-addon">
<input type="text" ng-model="selectedOptions.plants" uib-typeahead="plane as plane.formatted_address for plane in search($viewValue)" type-ahead-custom="maps1" typeahead-loading="loadingdata" typeahead-no-results="noResults" class="form-control ng-valid ng-dirty ng-valid-parse ng-touched" aria-autocomplete="list" aria-expanded="false" aria-owns="typeahead-4-8758" />
</div>
</div>
</div>
</div>
</form>
// 代码在这里
var exampleApp = angular.module('exampleApp', ['ui.bootstrap']);
exampleApp.directive('typeAheadCustom', function($http, $q) {
return {
link: function($scope, $element, $attributes) {
$scope.search = function(newValue) {
console.log($attributes.typeAheadCustom);
var dfr = $q.defer();
$http.get('//maps.googleapis.com/maps/api/geocode/json', {
params: {
address: newValue,
sensor: false
}
}).success(function(data) {
dfr.resolve(data.results);
});
return dfr.promise;
};
}
}
});
exampleApp.controller('myCtrl', function($http,$scope) {
$scope.selectedOptions = {};
});
【问题讨论】:
标签: angularjs angularjs-directive