This directive is used to determine, if given value is, or is not in datalist associated with it. It works perfectly when I type into the input, but it wont work if the datalist changes due to $digest cycle (adding values to it). If I then update the input, it will function 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;
};
}
};
});