Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am new on AngularJS and I got that error. Here is my code:

app.factory('NotificationService', function($http){
    var factory = {};
    factory.getNotificationList = function($http){
        var url = "http://some/url";
        return $http.get(url);
    }
    return factory;
});

app.controller('NotificationListController', function($scope,$http, NotificationService) {
    var notificationList = NotificationService.getNotificationList();
    notificationList.then(function(response){
        console.log(response);
        $scope.notificationData = response.data;
        return response;
    });
});

I am so confuse where my mistake is. The error message is:

TypeError: Cannot read property 'get' of undefined at Object.factory.getNotificationList (http://localhost:63342/EmailNotification/js/email-angular.js:15:21)

share|improve this question
1  
$http is declared twice within the factory. For the 2nd declaration, the named parameter for getNotificationList(), you aren't providing an argument to give it a value. – Jonathan Lonowski Mar 17 '16 at 3:59
up vote 0 down vote accepted

You are getting this error because $http is undefined in your factory.

You can fix it by passing it to the factory like so:

app.factory('NotificationService', ['$http', function ($http) {
    var factory = {};
    factory.getNotificationList = function() { // Remove the `$http` parameter from here.
        var url = "http://some/url";
        return $http.get(url);
    }
    return factory;
}]);
share|improve this answer
    
@twindicated70 accept it as an answer so if someone will encountered the same issue, he can easily check correct one – Sherlock Mar 17 '16 at 4:16
    
isn't this only necessary if they are minifying the js? – Paul Nikonowicz Mar 17 '16 at 4:23
1  
@PaulNikonowicz Yes, But it is consider as good practice. – Rajiv Pingale Mar 17 '16 at 12:09

Simple possible reason is $http is passed twice. When you pass any argument to the function, the closure for those variable changes

see the example here

var a =10;
var b= 20;

var abc = function()
{
      document.getElementById("first").innerHTML = a + b;
}

var pqr = function(a,b)
{
      document.getElementById("second").innerHTML = a + b;
}

abc();
pqr();

In this example, though variable a,b declared, second function pqr() will return NaN, until we pass an argument, explicitly

share|improve this answer
    
yes, thank you. it is very clear now :) – twindicated70 Mar 17 '16 at 6:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.