I created a directive for input elements which creates a custom select2 dropdown.
After selecting an element, the original input field (which is used to filter data via the ngModel) is filled with the selected data from the dropdown, but the change event of the input isn't fired.
If I change the input value by hand, the filter is working.
Here is the code of my directive.
.directive('ajaxSelect2', ['$timeout', function($timeout) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
switch(element.prop(tagName)) {
case 'INPUT':
$timeout(function() {
element.select2({
[ ... ] //the select2 part
});
});
break;
}
/* update the input value */
element.bind(change, function(e) {
scope.$apply(function() {
scope[attrs.ngModel] = e.val;
});
});
scope.$watch(element, function () {
console.log('x'); //not called
});
}
}
}])
I thought the element.bind(change)
in the scope.apply()
would trigger a view update, but it doesn't work.
Does anybody have an idea how to tell the ngModel it has new data?
EDIT: I figured out, the problem is the ngModel. Because I have to filter several values, my Model is filter.foobar
. This is not working. If I change the model to foobar
, it will work. I created a fiddle to illustrate: http://jsfiddle.net/StinsonMaster/6t3Nt/3/