2017-05-19 52 views
1

按鈕單擊我正在調用一個方法Getdata();但問題是第一次加載頁面時,如果我點擊按鈕,然後得到「HTTP錯誤」響應。但如果我再次點擊該按鈕,則會顯示成功響應。Angular HTTP在第一次點擊時不起作用

HTML

<button type="submit" class="btn btn-primary form-inline inline-Magrin" ng-click="Getdata()">Search</button> 

JS

$scope.Getdata = function() { 
    if (($scope.NumberOfrecords > $scope.TotalRecords) && ($scope.TotalRecords != 0)) { 
     alert("Search record count should not be greater than total records"); 
     return; 
    } 
    debugger; 
    $http({ 
     url: '/scrap/Resultdata', 
     method: "GET", 
     params: { 
      Searchbox: $scope.Searchbox, 
      Category: $scope.Category, 
      NumberOfrecords: $scope.NumberOfrecords 
     } 
    }).then(
     function successCallback(response) { 
      response = $scope.filterRecord(response); 
      debugger; 
      $scope.data = response.data.ResponseItems; 
      $scope.TotalRecords = response.data.TotalResults; 
      $scope.tableParams = new NgTableParams({ 
       page: 1, 
       count: 10 
      }, { 
       data: $scope.data 
      }); 
     }, 
     function errorCallback(response) { 
      debugger; 
      alert("error"); 
     }); 
} 

console image

+1

控制檯中的錯誤不是針對'/ scrap/Resultdata'的請求。看看你的網絡流量,看看請求有效和不起作用的區別。我猜你的一些'$ scope'變量沒有在第一次初始化 – rob

+0

嘗試'type =「button」'而不是'type =「submit」' – Jer

+0

你搞亂了你的url,但不是在插入代碼。 – neptune

回答

1

按鈕類型是 「提交」,應該是 「按鈕」

<button type="button" class="btn btn-primary form-inline inline-Magrin" ng-click="Getdata()">Search</button> 
1

如果您有提交類型,那麼你需要添加標籤在HTML,

和形式-------- 的OnSubmit =的GetData()

請嘗試使用type =按鈕代替

相關問題