Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:Factory.function不是函数_Javascript_Angularjs_Factory_Typeerror - Fatal编程技术网

Javascript TypeError:Factory.function不是函数

Javascript TypeError:Factory.function不是函数,javascript,angularjs,factory,typeerror,Javascript,Angularjs,Factory,Typeerror,我正在用web api编写我的第一个angular应用程序,在从工厂调用函数时遇到了一些问题 我有两个工厂,看起来像这样: main.factory('Table', function ($http, $log) { return { build: function (token, cubeid) { return $http({ method: 'POST', url: 'http:

我正在用web api编写我的第一个angular应用程序,在从工厂调用函数时遇到了一些问题

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

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', function ($http, $log) {
    return {
        authorize: function (username, password) {
            return $http({
                method: 'POST',
                url: 'path/to/api/',
                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: { 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', ['$scope', '$routeParams', '$log', '$http', 'Table', function tableController($scope, $routeParams, $log, 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);
    });
}]);
两个控制器如下所示:

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', function ($http, $log) {
    return {
        authorize: function (username, password) {
            return $http({
                method: 'POST',
                url: 'path/to/api/',
                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: { 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', ['$scope', '$routeParams', '$log', '$http', 'Table', function tableController($scope, $routeParams, $log, 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);
    });
}]);
出于某种原因,build函数会引发一个错误,称为“TypeError:Table.build不是函数”,而authorize函数的工作方式类似于一个符咒

有人能给我解释一下为什么构建函数不起作用吗


PS:我已经检查过令牌是否真的通过了控制器。

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

['$scope', '$routeParams', '$log', '$http', 'Table', 
function tableController($scope, $routeParams, $log, Table)
应该是

['$scope', '$routeParams', '$log', '$http', 'Table', 
function tableController($scope, $routeParams, $log, $http, Table)

您是指buildtable函数吗?因为这在您的表Factory中被称为普通的“构建”。对不起,它应该在两个位置都构建。这不是问题所在,它仍然不起作用