我有一个工厂方法......
(function (angular) {
"use strict";
angular
.module('app')
.factory('UserService', ['$rootScope', '$q', function ($rootScope, $q) {
var markCurrentUserAsContentOwner = function () {
var user = getCurrentUser(true);
user.set('isContentOwner',"true");
user.save(null, {
success: function(savedUser) {
alert("succes");
},
error: function(savedUser,error) {
alert("error");
}
});
};
}]);
})(angular);
现在如果我从另一个服务方法调用此方法..
(function(angular) {
'use strict';
angular
.module('app')
.service('ContentOwnerService',
[
'$q', 'UserService'
function($q, userService) {
var servicemethod=function() {
userService.markCurrentUserAsContentOwner();//UserService is the factory name
};
}]);
})(angular);
它显示错误。未捕获TypeError:undefined不是函数。 请任何人帮我解决此错误..
答案 0 :(得分:1)
使用$injector
服务解决此问题。
Example:
session.factory('redirectInterceptor', ['$injector','$rootScope', '$timeout', '$q', '$window', function($injector,$rootScope, $timeout, $q, $window) {
return {
'request': function(req) {
req.headers['CustomHeader'] = "Be Creative!";
return req || $q.when(req);
},
'response': function(response) {
if (response.data.Status === 'Failed...') {
var AuthenticationService = $injector.get('AuthenticationService');
AuthenticationService.ClearCredentials();
$window.location.href = "/#/login";
$timeout(function() {
$rootScope.sessiondata = true;
}, 5000)
return $q.reject(response);
} else {
return response;
}
}
}}])