AngularJS | How to define a boot process for a controller?

1

I have a full Ajax app that needs to get data via WebService when the page loads. I have already initialized via ng-init but I do not know how to send the controller "load" and execute the block of code that retrieves the data. When I enter the function in a ng-click and click on the element it works, but only so.

How do you define a boot process for a controller?

    
asked by anonymous 04.09.2015 / 03:17

2 answers

1

I saw that I just call a function of $scope in the directive ng-init that it does what I'm expecting:

<div ng-init="init()">
    blah
</div>

In JavaScript:

//...
$scope.init = function() {} // ...

This causes my controller to initialize and run the init function.

Reference: link

    
04.09.2015 / 03:24
0

First create a driver in your .js file

app.controller('controlador', function($scope){

   $scope.funcao = function(){
       //algoritmo.
   };    
});

Then in your html, declare this controller

<div data-ng-controller="controlador">
    //Html
</div>
    
04.09.2015 / 14:32