簡體   English   中英

TypeError:無法讀取AngularJS上未定義的屬性“ get”

[英]TypeError: Cannot read property 'get' of undefined on AngularJS

我是AngularJS的新手,但遇到了該錯誤。 這是我的代碼:

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;
    });
});

我很困惑我的錯誤在哪里。 錯誤消息是:

TypeError:無法讀取Object.factory.getNotificationList( http:// localhost:63342 / EmailNotification / js / email-angular.js:15:21 )中未定義的屬性“ get”

因為$http在您的工廠中未定義,所以您收到此錯誤。

您可以通過將其傳遞給工廠來修復它,如下所示:

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;
}]);

一個簡單的可能原因是$http傳遞了兩次。 將任何參數傳遞給函數時,這些變量的閉包都會更改

看到這里的例子

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();

在此示例中,盡管聲明了變量a,b,第二個函數pqr()將返回NaN,直到我們明確傳遞參數為止

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM