简体   繁体   中英

Angularjs how to preserve url in function?

Hi I am developing one Angularjs application. I have three cascading drop downs. Based on the selected values in drop down i am binding div with data received from api(div with ng-repeat). I have implemented paging.

On page load i am binding first dropdown.

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 is a function i am calling from different scenarios. For example, ng-change event of first dropdown

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

In paging i have below function. This is executed when i click on page numbers for example 1,2, etc

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

Here my problem starts. If i click on any page number $scope.pageChanged function executes. I will get page number to send it to server. after that i will call getcadetails(?). Now how can i get baseurl for getcadetails? Is there any way i can implement this in better way? Any help would be appreciated. Thank you.

i think u can define a variate for selectedMakeData , and define a function to create url. every time before invoke getcardetails() u should calculate the dynamicUrl .

code looks this:

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  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM