I am starting with AngularJS and i am having some issues when trying to use a factory from a controller.
I have the following factory
angular.module('testingApp')
.factory('factoryService', function ($http) {
// Service logic
var getSpec = function(p) {
return $http.get('http://someurl//?p=' + p);
};
return {
getSpec: getSpec
};
});
and then i try to consume it from the controller as follows
angular.module('testingApp')
.controller('ServiceincientsCtrl',[ function (factoryService,$scope) {
console.log('Starting Service Incident Controller');
factoryService.getSpec('AAA').then(function(response){
$scope.result = response.data;
}, function(error){
console.log('opsssss' + error);
});
}]);
But when i try to run it i receive the following message
TypeError: Cannot read property 'getSpec' of undefined
I don't know what i am missing,It should be a newbbie error, I googled it and i tried many examples with the same result.
Any ideas of what i am doing wrong?
Thanks!
We use arr && arr.length > 0 to make sure arr is defined and it has length bigger than 0. To fix Error: TypeError: Cannot read property ‘…’ of undefined with Angular, we should make sure the variable we’re rendering is defined.
TypeError: Cannot read properties of undefined (reading x) Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong to objects.
In JavaScript, properties and functions can only belong to objects. Since undefined is not an object type, calling a function or a property on such a variable causes the TypeError: Cannot read property of undefined.
Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong to objects. Since undefined is not an object type, calling a function or a property on such a variable causes the TypeError: Cannot read property of undefined.
Looks like you are not using the dependancy array notation properly. Please refer the below code. Please add 'factoryService
' & '$scope
' as array items.
.controller('ServiceincientsCtrl', ['factoryService', '$scope', function(factoryService, $scope) {
console.log('Starting Service Incident Controller');
factoryService.getSpec('AAA').then(function(response) {
$scope.result = response.data;
}, function(error) {
console.log('opsssss' + error);
});
}]);
Angular documentaion on dependancy injection.
First of all, you didn't declare your controller properly. It should look like this:
.controller('ServiceincientsCtrl',['$scope', 'factoryService', function($scope, factoryService) {
I personally use Service
s as I find them more readable.
Here's what your factory would look like as a Service
:
myApp.service('factoryService', function ($http) {
this.getSpec = function(p) {
return $http.get('http://someurl//?p=' + p);
}
});
This would work with your current controller.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With