2017-04-26 76 views
0

嗨,我正在開發一個Angularjs應用程序。我有三個級聯下拉。基於下拉列表中選定的值,我將綁定div與從api(div with ng-repeat)接收的數據綁定。我已經實施了分頁。Angularjs如何在函數中保留url?

在頁面加載我綁定第一個下拉菜單。

var arrMakes = new Array(); 
     $http.get(url + 'api' + '/Vehicle/' + 'GetVehicleMake').success(function (data) { 
      $.map(data.data, function (item) { 
       arrMakes.push(item); 
      }); 
      $scope.list = arrMakes; 
      var dynamicUrl = url + 'api' + '/Vehicle/' + 'Getcars/'; 
      //bind data to Div randomly. 
      getcadetails(dynamicUrl); 
     }).error(function (status) { 
     }); 
function getcadetails(baseurl) 
     { 
      var arrallcarDetails = new Array(); 
      $http.get(baseurl,{ params: $scope.pagingInfo }).success(function (data) { 
        $.map(data.data, function (item) { 
        arrallcarDetails.push(item); 
        }); 
        $scope.carDetails = arrallcarDetails; 
        $scope.pagingInfo.totalItems = data.totalcount; 
      }).error(function (status) { 
      }); 
     } 

getcadetails是我從不同場景調用的函數。例如,第一 下拉

$scope.getModel = function (selectedMake) { 
var selectedMakeData = selectedMake.ID; 
      var arrModel = new Array(); 
      $http.get(url + 'api' + '/Vehicle/' + selectedMakeData + '/GetVehicleModel').success(function (data) { 
       $.map(data.data, function (item) { 
        arrModel.push(item); 
       }); 
       $scope.Modellist = arrModel; 
       var dynamicUrl = url + 'api' + '/Vehicle/' + 'Getcars/' + '?MakeID=' + selectedMakeData; 
       //bind data to Div randomly. 
       getcadetails(dynamicUrl); 
      }).error(function (status) { 
      }); 
} 

的NG-改變事件在尋呼我有以下功能。這是執行時,我點擊頁碼例如1,2等

$scope.pageChanged = function (currentPage) { 
       $scope.pagingInfo.pageNumber = currentPage; 
       getcardetails(); 
      }; 

在這裏我的問題開始。如果我點擊任何頁面編號$ scope.pageChanged函數執行。我將得到頁碼發送給服務器。之後我會打電話給getcadetails(?)。現在我怎樣才能獲得getcadetails的baseurl?有什麼辦法可以以更好的方式實現嗎?任何幫助,將不勝感激。謝謝。

回答

0

我想你可以定義一個變量selectedMakeData,並定義一個函數來創建url。每次調用getcardetails()之前你都應該計算dynamicUrl

代碼如下這樣:

var selectedMakeData = 0; 

selection.addEventListener('change',() => { 
    selectedMakeData = newData; 
}); 

$scope.pageChanged = function (currentPage) { 
    var url = getUrl(); 

    $scope.pagingInfo.pageNumber = currentPage;   
    getcardetails(url); 
}; 

function getUrl() { 
    // return url baseed on selectedMakeData 
}