4

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)

1
  • 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. Commented Mar 17, 2016 at 3:59

2 Answers 2

3

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;
}]);
Sign up to request clarification or add additional context in comments.

2 Comments

@twindicated70 accept it as an answer so if someone will encountered the same issue, he can easily check correct one
isn't this only necessary if they are minifying the js?
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.