Sto provando a creare una direttiva che avvolge il plugin twitter typeahead. Quello che ho finora è:creazione di direttiva angular-js che aggiorna il modello ng
HTML:
<input ng-twitter-typeahead type="text" ng-model="participant" data="exampleData" />
{{ participant }}
Voglio che il valore per 'partecipante' essere aggiornato quando seleziono qualcosa sul typeahead. Lo stesso typeahead funziona correttamente, ma non riesco a catturare il valore selezionato. Di seguito è il 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.
}
};
});
Mi manca ovviamente qualcosa di fondamentale quando si tratta di AngularJS. Qualsiasi aiuto sarebbe molto apprezzato!
EDIT **
ho trovato la soluzione. Io sono a volte incapaci:
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;
});
})
}
};
});
Grazie! Stavo complicando il tutto, e ho imparato qualcosa dal battermi la testa. – user2205763