1

How is it possible to invoke a function when HTML page is loaded? Right now, the function call is made manually by pressing the button, like:

<p>
    {{address}}
</p>

<button class="btn btn-primary checkout" ng-click="getAddress()">
     <i class="fa"> Get address</i>
</button>

It is also possible to call function using {{getAddress()}}. But, as expected it causes infinite loop.

getAddress() makes call to a backend, gets data and populates address on $scope ($scope.address)

It would be nice to load it automatically and get rid off the button.

1
  • 2
    Why not just make the call in your controller? Commented May 14, 2015 at 17:00

1 Answer 1

2

In your controller itself you can invoke getAddress(). You might have controller like following

myapp.controller('yourController', function($scope){
        $scope.address = "";
        $scope.getAddress = function(){
           $scope.address = "some address"; 
        };
        //You can call here itself, it will get invoked when page and in  turn your script loads
        $scope.getAddress();
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.