0

I am very new to angularjs and require help on calling a function on url call. I am directly accessing the query params from url using

http://localhost/index.html#/?defined=true&var1=value1&var2=value2&

$location.search()['defined'];

now , within my controller I have a function 'caller' which is being called on events like ng-change , ng-model etc. on the html page which works fine

angular.module('ngAp').controller
('ngCntrl',function($scope,$http,$location{

    $scope.caller = function() {
       // code logic 
    }
}

What I am trying to do is to call the function 'caller' when I directly access the above url

$location.search()['defined'];

if (defined == "true" )
{ //call the caller function.

}

I have no idea how can I call the function when i directly call the url. Any help is greatly appreciated.

2
  • 1
    To call the function simply use $scope.caller(); Did you mean something else? Commented Mar 13, 2016 at 10:28
  • Thanks , it worked , but now I am running into a different problem.I have a call to $http.get() inside the controller which isnt getting called. Commented Mar 13, 2016 at 11:33

1 Answer 1

1

If you take a look at the docs for $location - https://docs.angularjs.org/api/ng/service/$location

It has some events you can use like $locationChangeStart. But it's better to bind to this event in angular.module('ngAp').run(...).

angular.module('ngAp').run(moduleRun);
function moduleRun ($location, $rootScope) {
  $location.on('$locationChangeStart', function (event, newUrl, oldUrl, newState, oldState) {
    var defined = $location.search()['defined'];
    $rootScope.defined = defined //you can assign it to $rootScope;
    if (defined === "true" ) {
      //call the caller function.
    }
  })
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.