I have a requirement to call another controller after the first one is loaded and rendered. Means I want to call controllers one-by-one, when one controller is successfully loaded and rendered then another controller is initiated.
Asked
Active
Viewed 76 times
0
-
http://stackoverflow.com/questions/9293423/can-one-controller-call-another – Furkan Başaran Dec 22 '15 at 12:13
-
@FurkanBaşaran but how to know my first controller has been loaded and rendered? – kumardippu Dec 22 '15 at 12:59
1 Answers
0
You need to use $broadcast, using this you can notify the other controller when something changes on your first controller.
Angular provides $broadcast, $on and $emit for this.
And if above is not fits your requirement you should use AngularJS stateprovider (ui-router and define states and bound your controller.
First call state1 which has controller1, if everything goes fine within that controller call state2 which has controller2
$state.go("state2");
var routerApp = angular.module('module-name', ['ui.router']);
routerApp.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/state1');
$stateProvider
.state('state1', {
url: '/state1',
templateUrl: 'state1.html',
controller : 'controller1'
})
.state('state2', {
url: '/state2',
templateUrl: 'state2.html',
controller : 'controller2'
})
});
Index.html
<div ui-view></div>
Controller like
routerApp.controller("controller1", ['$state', function($state){
//after
$state.go("state2");
}]);

Pratap A.K
- 4,337
- 11
- 42
- 79