This particular directive is designed to determine whether a given value exists within the associated datalist or not. It functions flawlessly when I input text into the field, but it fails to work properly if the datalist undergoes changes as a result of the $digest cycle (such as adding new values). However, if I then make an update to the input, it starts working correctly.
app.directive('list', function (){
return {
restrict: "A",
require: "ngModel",
priority: 100,
link: function(scope, elem, attr, ngModel){
var list;
//For DOM -> model validation
ngModel.$validators.list = function(value){
if(!list){
var options = document.getElementById(attr.list).options;
var list = [];
for(var i=options.length-1; i>=0; i--){
if(isString(options[i].getAttribute("valid"))){
if(options[i].label){list.push(options[i].label.toLowerCase())}
if(options[i].value){list.push(options[i].value.toLowerCase())}
}
};
}
var valid = (value||!value==""?list.indexOf(value.toLowerCase()) !== -1:true);
return (!list.length)||valid;
};
}
};
});