这是实现对附加在父指令范围内的对象的只读绑定的方法吗?
如果父级更改它的“$scope.thing”,我真的需要子指令接收更改通知。
基本上,我现在正在投票。我想要一个回调方法。
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>'
};
})
和强制性的plnkr: