burseaner
burseaner

Reputation: 905

Invoke function in HTML which is defined in controller

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.

Upvotes: 1

Views: 58

Answers (1)

C.P.
C.P.

Reputation: 1222

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();
});

Upvotes: 2

Related Questions