购物商城网站建设方案,青岛建站方案,怎样开网站,如何自己开一个平台有这样的一个需求#xff1a;添加用户的时候#xff0c;根据主键判断当前添加用户的email是否已经被使用。为此#xff0c;我们需要把主键和email来传递给远程的一个API#xff0c;让API返回结果#xff0c;告之当前email是否被使用过。写一个验证email唯一性的Directive添加用户的时候根据主键判断当前添加用户的email是否已经被使用。为此我们需要把主键和email来传递给远程的一个API让API返回结果告之当前email是否被使用过。写一个验证email唯一性的Directive页面大致如下表现 input typetext nameemail classform-controldata-ng-modelvm.customer.emaildata-ng-model-options{updateOn: blur, allowInvalid: true}data-my-uniquedata-my-unique-key{{vm.customer.id}}data-my-unique-propertyemaildata-ng-minlength3required /span classerrorMessage ng-showeditForm.email.$touched editForm.email.$error.uniqueEmail already in use
/span 以上data-my-unique-key用来接收主键data-my-unique-property用来接受email这个值。Directive部分大致如下 (function(){var injectParams [$q, dataService];var myUniqueDirective function($q, dataService){var link function(scope, element, attrs, ngModel){ngModel.$asyncValidators.unique function(modelValue, viewValue){var deferred $q.defer(),currentValue modelValue || viewValue,//获取主键key attrs.myUniqueKey,//my-unqiue-key {{customer.id}}//获取emailpropertyattrs.myUnqiueProperty; //my-unique-propertyemailif(key property){dataService.checkUniqueValue(key, property, currentValue).then(function(unique){if(unique){deferred.resolve();} else {deferred.reject();}});return deferred.promise;} else {return $q.when(true);}}};return {restrict: A,require: ngModel,link: link}};myUniqueDirective.$inject injectParams;angular.module(customersApp).directive(myUnique, myUniqueDirective);
}()); 转载于:https://www.cnblogs.com/darrenji/p/5158145.html