TypeError: Factory.function is not a function
I am writing my first angular app with web api and am having some problems with calling functions from factory.
I have two factories that look like this:
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 }
});
}
};
});
And two controllers that look like this:
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);
});
}]);
For some reason the build function raises an error: "TypeError: Table.build is not a function" while the authorization function works like a charm.
Can someone explain to me why the build function is not working?
PS: I have verified that the token is indeed passing the pass to the controller.
+3
source to share
1 answer
you inject various services / factories into the controller.
['$scope', '$routeParams', '$log', '$http', 'Table',
function tableController($scope, $routeParams, $log, Table)
it should be
['$scope', '$routeParams', '$log', '$http', 'Table',
function tableController($scope, $routeParams, $log, $http, Table)
+10
source to share