只是尝试重构我的代码并打算使用以下结构:
服务:
} }]).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;
}
}}])

指令:
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;
}
}
}
}])

目前,由于我对承诺缺乏了解,我只是在努力解决问题。
致电bob= UsernameValidationHelper.usernameExists("bob");
时,我会收到Type Error: Undefined is not a function
我做错了什么?
答案 0 :(得分:0)
三次注射,但指令中只有两种(不同)参数:
['UsernameRESTService','CarrierValidationHelper','$q',function(UsernameValidationHelper,$q){}]