考虑下面的嵌套指令。我一直在尝试在嵌套指令的隔离范围之间绑定变量,并且无法实现双向绑定。
在示例中,我希望outerPower
绑定到innerPower
,并在innerPower
数字递增时更改。
检查小提琴http://jsfiddle.net/hally9k/sqea6Ln7/
testApp.directive('innerDirective', function () {
return {
scope: {
innerPower: '&'
},
template: '<div><p>Inner Power = {{innerPower}}</p>' +
'<button ng-click="increment();">Power up!</button>' +
'</div>',
controller: function ($scope, $element, $attrs) {
$scope.increment = function() {
++$scope.innerPower;
}
}
};
});
testApp.directive('outerDirective', function () {
return {
scope: {
},
template: '<div><p>Outer Power = {{outerPower}}</p>' +
'<div inner-directive inner-power="outerPower"></div>' +
'</div>',
controller: function ($scope, $element, $attrs) {
$scope.outerPower = 0;
}
};
});
答案 0 :(得分:2)
答案 1 :(得分:1)
只需更改单行
即可scope: {
innerPower: '=' //instead of &
}