Search code examples
angularjsangularjs-scopeangular-controller

Method call on page load when URL params exist in Angular JS is not working


I have the following requirement :

Read URL parameters, load the 2 drop downs on the page and prepopulate them with the values in URL params. And then call a method with the dropdown values to load a grid.

Here is what i did until now :

On the controller I am checking for params using $location.search() and if they exist, calling some methods. I put this code at the end of the controller. But when a method inside the controller is called , the REST call does not go through, instead the call goes through after the total controller load happens.

Appreciate any help!

example scenario:

 function abc(){/*some code*/}
 function xyz(){/*some code*/}
 function abd(){/*some code*/}
 if(typeof $location.search().paramaName !=undefined){
  /*Read URL params. preselect the current drop down by calling abc,xyz.use the values to call abd() which makes a REST API call to load the data on the page. */

 }

Solution

  • You are probably missing the point that these rest calls are asynchronous.

    You could include the call to these functions in the then() method of the returned promise object; which will be executed once the async call completes.

    More on promises here -
    Angular Promises
    Promises Explained