locked
Angularjs - How can i pass get or post url as a parameter ? RRS feed

  • Question

  • User-1846805900 posted

    Hi

    i need to use js code in about 10 pages - but each page will have get url not as other as example:

    page 1: will use get url: /api/Ready
    page 2: will use get url: /api/NotReady
    page 3: will use get url: /api/Done
    page 4: will use get url: /api/waiting list

    all will use that same js code:

    var app = angular.module('jqanim', []);
    
    app.controller('InvoiceController', [
                       '$scope', '$http', function ($scope, $http) {
                           $http.get("/api/NoReady") // this is url needed to pass as parameter ?
                               .success(function (data) {
                                   var dataByMonth = {};
                                   data.forEach(itemToMonth);
                                   $scope.customersByMonth = dataByMonth;
    
                                   function itemToMonth(item) {
                                       item.MirageDate = new Date(item.MirageDate)
                                       var month = item.MirageDate.getMonth();
                                       var year = item.MirageDate.getFullYear();
                                       dataByMonth[year + '-' + month] = dataByMonth[year + '-' + month] || [];
                                       dataByMonth[year + '-' + month].push(item);
                                   }
                               });
                       }
    ]);

    so please can i get help about that with sample example to learn how can i do it ?

    Thanks a lot ...

    Tuesday, June 30, 2015 5:34 AM

Answers

  • User1644755831 posted

    Hello a.amin,

    You can create a global array which will hold all these urls. and then pass the index as urlid in the parameter.

    //globalarray.     
    var array = ['/api/Ready', '/api/NotReady', '/api/Done', '/api/waiting list']
    //function to get parameter by name function getParameterByName(name) { name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search); return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); } var url = array[parseInt(getParameterByName('urlid'))]; alert(url);

    Now you can pass the index in the url as suppose your page 1 needs to use ready call then http://localhost/Home/Page1?urlid=0 you can passit like this.

    then put url variable in the $http.get

    $http.get(url) // this is url needed to pass as parameter ?
    

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 1, 2015 2:43 AM