2015-04-06 17:50:08 +02:00
|
|
|
# .cadoles-slide-title[L'objet $scope (3/4)]
|
|
|
|
|
|
|
|
**Communication inter-controlleurs, évènements & propagation**
|
|
|
|
```html
|
|
|
|
<html>
|
|
|
|
<body ng-app="myApp">
|
|
|
|
|
|
|
|
<div ng-controller="ParentCtrl">
|
|
|
|
<div ng-controller="ChildCtrl">
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<script src="angular.js"></script>
|
|
|
|
<script>
|
|
|
|
var myApp = angular.module('myApp', []);
|
|
|
|
|
|
|
|
myApp.controller('ParentCtrl', ['$scope', function($scope) {
|
|
|
|
|
2015-04-08 23:22:53 +02:00
|
|
|
$scope.$on('child-event', function(evt) { console.log('Emitted event: ', evt); });
|
|
|
|
$scope.$broadcast('parent-event', {data: 'Parent event !'});
|
2015-04-06 17:50:08 +02:00
|
|
|
|
|
|
|
}]);
|
|
|
|
|
|
|
|
myApp.controller('ChildCtrl', ['$scope', function($scope) {
|
|
|
|
|
2015-04-08 23:22:53 +02:00
|
|
|
$scope.$emit('child-event', {data: 'Child event !'});
|
|
|
|
$scope.$on('parent-event', function(evt) { console.log('Broadcasted event: ', evt); });
|
2015-04-06 17:50:08 +02:00
|
|
|
|
|
|
|
}]);
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
```
|