Angular.module (' Com.ngnice.app '). Directive (' Bffielderror ',functionBffielderror ($compile) {return { //restricted to be used only in the form of attributes (Attribute).Restrict: ' EA ', //there must be a Ng-model attribute on this element, and if not, it will be an error .Require: ' Ngmodel ', //The link function is automatically executed when the current instruction is initializedLinkfunction(scope, element, Attrs, Ngmodel) {varSubscope = scope.$New(true); Subscope.haserror=function() { returnNgmodel. $invalid &&Ngmodel. $dirty; }; Subscope.errors=function() { returnNgmodel. $error; }; //compile a piece of HTML into a "live dom" and then pass the subscope to it, and the live DOM will automatically update itself with changes to Subscope varhint = $compile (' <ul ng-if= ' Haserror () "><li ng-repeat=" (name, wrong) in errors () ">{{name|error}</li ></ul> ') (Subscope); //Append the live dom to the current element so that it appearselement.after (hint); } };});
Angular.module (' Com.ngnice.app '). Constant (' Errors ', { ' not a valid format e-mail address ', ' This entry cannot be empty '});
function (Errors) { returnfunction (name) { return Errors[name] | | name;} );
How to optimize form validation