簡體   English   中英

如何從角度控制器調用角度Ajax函數

[英]How to call to angular ajax function from angular controller

這是我的代碼

$scope.GetLocatoinByCountry();

$scope.GetLocatoinByCountry = function () {
        $http({
            method: 'POST',
            url: '/JobPost/GetLocationByCountry',
            data: { countryId: $scope.draftJobPost.countryID }
        }).then(function successCallback(response) {
            var loc = response.data;
            $scope.locations = response.data;
        }, function errorCallback(response) {
            var error = response;
        });
    };

但出現錯誤

TypeError:$ scope.GetLocatoinByCountry不是一個函數

如何解決這個問題

將調用函數移到分配下方:

$scope.GetLocatoinByCountry = function() {
  $http({
    method: 'POST',
    url: '/JobPost/GetLocationByCountry',
    data: {
      countryId: $scope.draftJobPost.countryID
    }
  }).then(function successCallback(response) {
    var loc = response.data;
    $scope.locations = response.data;
  }, function errorCallback(response) {
    var error = response;
  });
};

$scope.GetLocatoinByCountry(); // move it here

TypeError:$ scope.GetLocatoinByCountry 不是一個函數

錯誤說明了一切。

問題是您要在$scope不存在的函數中調用該函數,然后再將該函數分配給$scope

我認為使用Angular 1時,可以使用$ http或$ resouce調用API。

在您的情況下:移動$scope.GetLocatoinByCountry();$scope.GetLocatoinByCountry(); 從上到下可以使其正常工作。

暫無
暫無

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

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