使用工厂内部路由控制器

时间:2015-09-18 21:06:12

标签: javascript ajax angularjs

我有这样的工厂:

[PFObject]?

当我试图从我的控制器那里打电话给这个工厂时:

angular.module('commentsApp')
        .factory("getCommentsFactory", getCommentsFactory);

function getCommentsFactory(scope, $http)
{
    return {
        getComments: getComments
    };

    function getComments()
    {
        return $http.get("/api/comments/" + scope.param)
                .then(getCommentsComplete)
                .catch(getCommentsFailed);

        function getCommentsComplete(data) {
        //GET ALL USER AND COMMENTS TOGETHER
            return data;
        }

        function getCommentsFailed(error) {
            scope.setLoading(false);
            return error;
        }
    }

}

我收到错误:

  

错误:[$ injector:unpr]   http://errors.angularjs.org/1.4.3/ $注射器/ unpr?P0 = scopeProvider%20%3C-应付%20%3 C-%20getCommentsFactory       在错误(本机)       在https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:6:416       在https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:40:375       在Object.d [as get](https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:38:364)       在https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:40:449       在d(https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:38:364)       at Object.e [as invoke](https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:39:124)       at Object。$ get(https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:37:98)       at Object.e [as invoke](https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:39:156)       在https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:40:478

任何人都知道什么是问题?

1 个答案:

答案 0 :(得分:2)

问题是你在你的工厂里注射了一种叫做“范围”的东西,但你没有可以在那里注射的范围。

如果您需要控制器中的值,则应将其作为参数传递给函数。您无法在工厂中注入控制器范围。

相关问题