Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
191
rated 0 times [  197] [ 6]  / answers: 1 / hits: 17000  / 7 Years ago, tue, march 21, 2017, 12:00:00

I have the following code in my service:



testApp.service('detailsService',['databaseService', 'loggedService', '$http', function(databaseService, loggedService, $http){

var details;

this.getDetails = function(name){

return $http({
method : GET,
url : name
}).then(function(response) {
details= response.data;
console.log(response.data);
return response.data;
});

};

}]);


What i want to do is call this function in my controller when the page(view) is loaded.



testApp.controller('testController', ['$scope', '$location', 'databaseService','detailsService', '$routeParams', function($scope, $location, databaseService, $routeParams, detailsService){

$scope.details;

var selectedDetails = function(name){
detailsService.getDetails(name).then(function(data){
$scope.details= data;
});
};

selectedDetails(name);

}]);


I keep getting the error detailsService.getDetails is not a function.
I'm using the same function from the detailsService in another controller without any problems.



Does anybody know why i keep getting this error?


More From » angularjs

 Answers
26

The error is expected as you are not injecting dependencies properly, You need to use the correct sequence.



testApp.controller('testController', ['$scope', 
'$location',
'databaseService',
'detailsService',
'$routeParams',
function($scope, $location, databaseService, detailsService, $routeParams ){


instead of



testApp.controller('testController', ['$scope', 
'$location',
'databaseService',
'detailsService',
'$routeParams',
function($scope, $location, databaseService, $routeParams, detailsService){


Note Both the string part and the function arguments need to match up 1:1.


[#58465] Saturday, March 18, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hallie

Total Points: 503
Total Questions: 114
Total Answers: 103

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
;