Last active
August 29, 2015 14:25
-
-
Save seemaullal/f8d2b47d036ff9b6308a to your computer and use it in GitHub Desktop.
Angular Sample #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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