Skip to content

Instantly share code, notes, and snippets.

@seemaullal
Last active August 29, 2015 14:25
Show Gist options
  • Save seemaullal/f8d2b47d036ff9b6308a to your computer and use it in GitHub Desktop.
Save seemaullal/f8d2b47d036ff9b6308a to your computer and use it in GitHub Desktop.
Angular Sample #1
/*
This is a custom attribute directive I wrote. It was used for form validation to ensure that the name
entered in a form was not already in the database. It is an async custom validator.
*/
'use strict';
app.directive('sandwichnamevalidator', function(SandwichesFactory, $q){
return {
require: 'ngModel',
restrict: '',
link: function(scope, elm, attrs, ctrl) {
ctrl.$asyncValidators.sandwichnamevalidator = function(modelValue,viewValue) {
var sandwiches;
return SandwichesFactory.getSandwiches().then(function (sandwichArr) {
sandwiches = sandwichArr;
var names = sandwichArr.map(function(sand) {
return sand.name;
});
if (names.indexOf(viewValue) > -1) {
console.log("you can't submit. sandwich name must be unique");
return $q.reject("Not unqiue");
}
else {
return true;
}
}).catch(function(err) {
return $q.reject("Error");
});
};
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment