formations/javascript/angular/scope-3.md

35 lines
866 B
Markdown
Raw Permalink Normal View History

# .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 !'});
}]);
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); });
}]);
</script>
</body>
</html>
```