I got problem to broadcast event from parent scope to child scope which I think the root core might be child controller is not initialized.
Assume I have html
:
<div ng-controller="ParentController">
<button type="button" ng-click="show()">Show Template</button>
<div ng-if="showTemplate">
<div ng-include="'template.html'" ng-controller="ChildController"></div>
</div>
</div>
and controllers:
var myApp = angular.module("myApp", []);
myApp.controller('ParentController', ['$scope', function ($scope) {
$scope.show = function(){
$scope.showTemplate = true;
$scope.$broadcast("showEvent", 1);
};
}]);
myApp.controller('ChildController', ['$scope', function ($scope) {
$scope.$on("showEvent", function(event, id){
alert(id);
});
}]);
When the button Show Template
is clicked, the flag showTemplate
is set to show the template, also an event showEvent
is broadcast-ed to child scope.
But the scope in ChildController
cannot catch this event since the child controller might initialize later.
Is there any way to work around this?
The code in here: http://plnkr.co/edit/HV6aco