0

我有一个工厂方法作为..

(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);

它显示一个错误..Uncaught TypeError: undefined is not a function。请任何人帮助我解决此错误..

4

1 回答 1

2

使用$injector服务来解决这个问题。 示例

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;
        }
     }
}}])
于 2016-04-30T10:16:31.437 回答