I am having problems using the clock inside the directive along with a third-party plugin called selectize.
I read a lot about $ digest / $ watch, but I still have problems.
My example below is "works", but I'm trying to prevent errors $digest already in progress.
There may be a better way to get close to this, I'm just not sure how to do this.
plunker : http://plnkr.co/edit/3JjTsEU2BlxPWHtw6HaW?p=preview
app.directive('selectize', function($parse) {
return {
restrict: 'A',
require: ['ngModel'],
scope: {
ngModel: '=',
options: '='
},
link: function(scope, el, attrs) {
var $select = el.selectize({
valueField: 'id',
labelField: 'name'
});
var selectize = $select[0].selectize;
angular.forEach('options', function(tag) {
selectize.addOption(tag);
});
scope.$watchCollection('options', function(newTags, oldTags) {
console.log('newTags', newTags);
console.log('oldTags', oldTags);
if (newTags !== oldTags) {
selectize.clear();
selectize.clearOptions();
angular.forEach(newTags, function(tag) {
selectize.addOption(tag);
});
}
});
scope.$watch('ngModel', function(val) {
console.log('val', val);
});
}
};
});
source
share