In my webpage there is main controller that needs some initialization to be done using services by calling data from server using $http or getJSON.
There are other child controllers in page that should not be called once main controller is done with data loading.
How to inject DI in controllers?
Edit1 ex:
something similar to Call Angular controller from script onload
where service calls server to get some data and once it finishes notify others about it.
Is this possible?
Asked
Active
Viewed 624 times
0

Community
- 1
- 1

user2025527
- 109
- 2
- 12
-
I don't understand what you mean by "other child controllers in page should not be called". Can you explain some more? – Mark Rajcok Apr 25 '13 at 03:56
-
edited main question for example – user2025527 Apr 25 '13 at 04:43
-
If you want your views to update when data comes in from the server, you might not have to notify them... Angular can do this for you automatically. In a controller, set a $scope property to reference a service property and then {{use}} it in your view. When the data comes back from the server, Angular will notice the change and update your view. See [this question](http://stackoverflow.com/questions/15342672/angularjs-using-a-service-as-a-model-ng-repeat-not-updating) for an example. – Mark Rajcok Apr 26 '13 at 02:23
1 Answers
2
Looks like you should look into the $scope.$emit, $scope.$broadcast, and $scope.$on methods to handle events in angularjs.
Take a look at the angular scope guide.
The basic idea might look something like this:
$http.get("/api/stuff", function(data)
.then(function(data){
$scope.$broadcast("apiLoad", data);
}, function(err){
//do something with err
})
then a child controller can use:
$scope.$on("apiLoad", function( event, data){
//do something now that apiLoad is done.
})

Ryan Q
- 10,273
- 2
- 34
- 39