【发布时间】:2014-08-07 17:29:37
【问题描述】:
我创建了这个简单的 plunker 来演示这个问题:
http://plnkr.co/edit/xzgzsAy9eJCAJR7oWm74?p=preview
var app = angular.module('app',[]);
app.controller('ctrl',function($scope){
$scope.items = {};
})
app.directive('myDirective',function(){
return {
restrict: 'E',
scope: {
item: "=item"
},
template: "<h2>ng-repeat: </h2>" +
"<button ng-repeat='i in [1,2,3]' ng-click='item = true'>Set to true</button>" +
"<h2>no ng-repeat: </h2>" +
"<button ng-click='item = false'>Set to false</button>"
}
})
<body ng-controller='ctrl'>
<h1>Item: {{items.someItem}}</h1>
<my-directive item='items.someItem'></my-directive>
</body>
当我将模型传递给指令时,数据绑定有两种方式工作,除非它是从 ng-repeat 内部访问的。 为什么会发生这种情况以及如何解决这个问题?
【问题讨论】:
标签: angularjs angularjs-directive angularjs-scope angularjs-ng-repeat