Twitter typeahead プラグインをラップするディレクティブを作成しようとしています。私がこれまでに持っているものは次のとおりです。
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;
});
})
}
};
});