【发布时间】:2016-10-06 14:58:05
【问题描述】:
对我的作用域变量foo 的更改将在 html 中更新。当该值在指令的控制器范围内发生变化时,它不会在 html 中更新。
我需要做什么才能更新?
我有一个简单的example:
app.js
var app = angular.module('app', []);
app.controller('ctrl', function($scope) {
$scope.foo = 99;
$scope.changeValue = function() {
$scope.foo = $scope.foo + 1;
}
});
app.directive('d1', function(){
return {
restrict: 'E',
scope: {
theFoo: '='
},
templateUrl: 'd1.html',
controller: 'd1Ctrl',
}
});
app.controller('d1Ctrl', function($scope) {
$scope.test = $scope.theFoo;
});
d1.html
<div>
<p>The value of foo is '{{theFoo}}'.</p>
<p>The value of test is '{{test}}'.</p>
</div>
在 index.html 中
<d1 the-foo='foo'>
</d1>
<button ng-click='changeValue()'>change value</button>
总而言之,{{theFoo}} 正在更新,但 {{test}} 没有。为什么?
【问题讨论】:
标签: angularjs angularjs-directive angularjs-scope angular-controller