只是试图重构我的代码并打算具有以下结构:
服务:
} }]).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
我究竟做错了什么?