TypeError: Factory.function is not a function
TypeError: Factory.function is not a function
我正在用 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);
});
}]);
出于某种原因,构建函数会引发错误 "TypeError: Table.build is not a function",而授权函数却非常有效。
任何人都可以向我解释为什么构建功能不起作用吗?
PS:我已经检查过令牌是否确实传递给了控制器。
你向你的控制器注入了不同的services/factories
['$scope', '$routeParams', '$log', '$http', 'Table',
function tableController($scope, $routeParams, $log, Table)
应该是
['$scope', '$routeParams', '$log', '$http', 'Table',
function tableController($scope, $routeParams, $log, $http, Table)
我正在用 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);
});
}]);
出于某种原因,构建函数会引发错误 "TypeError: Table.build is not a function",而授权函数却非常有效。
任何人都可以向我解释为什么构建功能不起作用吗?
PS:我已经检查过令牌是否确实传递给了控制器。
你向你的控制器注入了不同的services/factories
['$scope', '$routeParams', '$log', '$http', 'Table',
function tableController($scope, $routeParams, $log, Table)
应该是
['$scope', '$routeParams', '$log', '$http', 'Table',
function tableController($scope, $routeParams, $log, $http, Table)