In the code below you can see I have a counter component that fires events on increment and decrement and those are bound to an output that is displayed. These events are on $rootScope so they can be heard on another component which works too. What doesn't work is the data binding on my watchercomponent. If you look at the console, it's receiving the event and its data properly and is even updating the message property but it isn't being update in the view. What gives? Below is my code:
Script.js
angular.module('testApp', []).component('counter', {
bindings: {
input: '<',
output: '&'
},
controller: function ($rootScope, $attrs) {
this.output = $attrs.input;
this.increment = function increment() {
this.output++;
console.log("event from counter fired");
$rootScope.$emit('eventfromcounter', 'increment');
}
this.decrement = function decrement() {
this.output--;
console.log("event from counter fired");
$rootScope.$emit('eventfromcounter', 'decrement');
}
},
template: function ($element, $attrs) {
return [
'<input type="text" ng-model="$ctrl.output">',
'<button type="button" ng-click="$ctrl.decrement();">-</button>',
'<button type="button" ng-click="$ctrl.increment();">+</button>',
].join('');
}
}).component('watchercomponent', {
bindings: {
message : '&'
},
controller: function ($rootScope, $attrs)
{
this.message = 'waiting for event';
$rootScope.$on('eventfromcounter', function (event, args)
{
console.log("recieved at anothercomponent : " + event.name + " : " + args);
this.message = args;
console.log("message : " + this.message);
});
},
template: function($element, $attrs)
{
return [
'<br/><span>watchercomponent : {{$ctrl.message}}</span>'
].join('');
}
});
index.html
<html>
<head>
<script src="angular.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-app = "testApp">
<counter input="3"></counter>
<watchercomponent></watchercomponent>
</div>
</body>
</html>
Plunker here.
Also are $rootScope events good practice to share events/data between components? If not could anyone point me into the right direction?
Thanks