Skip to content

Instantly share code, notes, and snippets.

@mbalex99
Created August 26, 2014 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbalex99/19acc7cc359be93c00d6 to your computer and use it in GitHub Desktop.
Save mbalex99/19acc7cc359be93c00d6 to your computer and use it in GitHub Desktop.
ExpressJs Validation AngularJS Directive Example
/**
* Created by maximilianalexander on 8/15/14.
*/
angular.module('app').directive('validationMessage', function(){
return {
restrict: 'E',
templateUrl: 'client/partials/validation-message.html',
scope: {
errorModels: "=",
errorKey: "@"
},
link: function(scope, element, attrs){
scope.$watchGroup(['errorModels', 'errorKey'], function(newValues, oldValues, scope){
var models = newValues[0];
var key = newValues[1];
if(!models || !key){
return;
}
var specificModel = models[key];
var formGroup = element.closest('.form-group');
if(specificModel){
scope.message = specificModel.msg;
formGroup.addClass('has-error');
}else{
formGroup.removeClass('has-error');
}
});
}
}
});
<span class="help-block" ng-show="message"><i class="fa fa-times"></i> {{message}}</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment