我正在用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:我已经检查过令牌实际上是通过控制器传递的.