我正在嘗試創建一個包裝twitter typeahead插件的指令。我至今是:創建angular-js指令,更新ng-model
HTML:
<input ng-twitter-typeahead type="text" ng-model="participant" data="exampleData" />
{{ participant }}
我想,當我選擇的預輸入的東西被更新爲「參與者」的值。打字頭本身工作正常,但我無法捕獲選定的值。下面是javascript:
var app = angular.module('myApp', [])
app.directive('ngTwitterTypeahead', function() {
return {
restrict: 'EA',
scope: {
data: '='
},
link: function ($scope, $element, $attrs) {
$element.typeahead($scope.data);
$element.bind('typeahead:selected', function(obj, datum) {
// I really don't know how to do this part
// the variable 'datum' is what I want to be passed to ng-model
// I tried things like:
// Including the ngModelController and typing:
// ngModel.$setViewValue(datum)
// but that didn't work.
}
};
});
我明顯失去了AngularJS的基本內容。任何幫助將不勝感激!
編輯**
我找到了解決辦法。我有時很無能:
angular.module('siyfion.ngTypeahead', [])
.directive('ngTypeahead', function() {
return {
restrict: 'C',
scope: {
datasets: '=',
ngModel: '='
},
link: function ($scope, $element, $attrs) {
$element.typeahead($scope.datasets);
$element.bind('typeahead:selected', function(obj, datum) {
$scope.$apply(function() {
$scope.ngModel = datum;
});
})
}
};
});
謝謝!我把整件事情搞得一團糟,而且我從這個問題上學到了一些東西。 – user2205763