[英]Angular js $asyncValidator “Undefined is not a function!” when passing promises
Just trying to refactor my code and intend to have the following structure: 只是试图重构我的代码,并打算具有以下结构:
Services: 服务:
} }]).factory('UsernameRESTService', ['Restangular',function(Restangular){ var restAngular = Restangular.withConfig(function(Configurer) { Configurer.setBaseUrl('http://localhost:2000/'); }); return { getCarrier: function(carrierCode) { var usernamePromise=restAngular.one(carrierCode).get(); return usernamePromise; } }}]).factory('UsernameValidationHelper',['UsernameRESTService','$q',function(UsernameRESTService,$q){ return{ usernameExists: function(username){ var deferred = $q.defer(); deffered.resolve(true); return deferred.promise; } }}])
Directive: 指示:
angular.module('myApp.commonDirectives', ['myApp.services']).directive('username', ['UsernameValidationHelper','$q',function(UsernameValidationHelper,$q){ return { restrict: 'A', require: "ngModel", link: function(scope, element, attrs, ctrl) { ctrl.$asyncValidators.username = function(value){ var deferred = $q.defer(); bob= UsernameValidationHelper.usernameExists("bob"); defer.resolve(); return deferred.promise; } } } }])
At the moment I'm just trying things out given my lack of knowledge of promises. 目前,由于我对诺言的了解不足,我只是在尝试一些事情。
When calling bob= UsernameValidationHelper.usernameExists("bob");
当调用bob= UsernameValidationHelper.usernameExists("bob");
I get a Type Error: Undefined is not a function
我收到Type Error: Undefined is not a function
What am I doing wrong? 我究竟做错了什么?
三次注入,但指令中仅两个(不同)参数:
['UsernameRESTService','CarrierValidationHelper','$q',function(UsernameValidationHelper,$q){}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.