0

In case of angular, in its life cycle when does the controller that we define using the .controller method get executed?

1 Answer 1

1

First, when you are accessing a DOM with ng-controller attached to it.

E.g.

<ul ng-controller="YourCtrl">
   <li ng-repeat="name in names">
       {{name.firstName}}
   </li>
</ul>

Documentation: https://docs.angularjs.org/guide/controller


Second, when you are accessing a URL route using $routeProvider / $stateProvider that has the method when() / state() with the parameter controller.

E.g.

Using ngRoute:

$routeProvider
     .when('/', {
         templateUrl: 'app/views/home.html',
         controller: 'homeCtrl'
     })
     .otherwise({
         redirectTo: '/'
     });

Using ui.router:

$stateProvider
     .state('home', {
       url: '/home',
       templateUrl: 'partial-home.html',
       controller: 'homeCtrl'
     });


Hope it helps.

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.