Is this the way to achieve read-only binding to an object attached in a parent directive's scope?
I really need the child directive to receive change notifications if the parent changes it's "$scope.thing".
Basically, I'm polling right now. I'd like a callback method.
angular.module('plunker', [])
.directive('myParent', function($compile) {
return {
controller: function($scope, $log) {
$scope.thing = { awesome: 'sucky' };
var count = 0;
setInterval(function() { $scope.thing = { awesome: 'cool'+count++ }}, 2000);
},
template: '<div><div my-child entity="thing"></div></div>'
};
})
.directive('myChild', function($parse, $log) {
return {
controller: function($scope) {
},
link: function(scope, element, attrs) {
var expression = attrs['entity'];
scope.onetime = $parse(expression)(scope);
$log.log('onetime');
$log.log(scope.onetime);
setInterval(function () {
if (expression) {
scope.intervaled = $parse(expression)(scope);
scope.$apply();
$log.log('interval');
$log.log(scope.intervaled);
}
}, 2000);
},
template: '<span>{{onetime}}</span>-<span>{{intervaled}}</span>'
};
})
And the obligatory plnkr: