微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – TypeError:Factory.function不是函数

我正在用web api编写我的第一个有角度的应用程序,我在调用工厂函数方面遇到了一些问题.

我有两个看起来像这样的工厂:

main.factory('Table',function ($http,$log) {
    return {
        build: function (token,cubeid) {
            return $http({
                method: 'POST',url: 'http://localhost:50051/api/structure/cube',headers: { 'Content-Type': 'application/x-www-form-urlencoded' },transformRequest: function (obj) {
                    var str = [];
                    for (var p in obj)
                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                    return str.join("&");
                },data: { token: token,cubeId: cubeid }
            });
        }
    };
});

main.factory('Login',$log) {
    return {
        authorize: function (username,password) {
            return $http({
                method: 'POST',url: 'path/to/api/',data: { username: username,password: password }
            });
        }
    };
});

两个看起来像这样的控制器:

main.controller('loginController',['$scope','$log','$http','$location','Login',function jobListController($scope,$log,$http,$location,Login) {

    $scope.login = function () {
        Login.authorize($scope.username,$scope.password).success(function (response) {
            $location.path('/table/'+response.token);
        });
    }

}]);

main.controller('tableController','$routeParams','Table',function tableController($scope,$routeParams,Table) {
    var cube = 130;
    var token = $routeParams.token;
    $log.log($routeParams.token);
    Table.build(token,cube).success(function (response) {
        $scope.structure = response;
        $log.log(response);
    });
}]);

由于某种原因,构建函数引发错误,说“TypeError:Table.build不是函数”,而授权函数就像魅力一样.

任何人都可以向我解释为什么构建函数不起作用?

PS:我已经检查过令牌实际上是通过控制器传递的.

解决方法

您将不同的服务/工厂注入您的控制器

['$scope',Table)

应该

['$scope',Table)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐