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

Asked

Viewed 987 times

1

I have an app full of Ajax that needs to get data via Webservice when the page loads. I’ve already initialized via ng-init but I don’t know how to have the controller "load" and execute the code block that retrieves the data. When I insert the function into a ng-click and click on the element it works, but only so.

How do you set a boot process for a controller?

2 answers

1

0

First create a controller in your file . js

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

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

then in your html, declare this controller

<div data-ng-controller="controlador">
    //Html
</div>
  • Post an explanation of what your code does and how it does.

  • 1

    as the user only asked to show how to initialize a controller, I just added the code, I thought I didn’t need the @Ricardo explanation

  • Renandegrandi posting an explanation the answer may be useful to more members than the author of the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.