【发布时间】:2023-06-02 14:51:02
【问题描述】:
请查看演示插件了解详细信息。
演示 Plunker
Example1 的 html 看起来像这样
<tr ng-repeat="class in Classes">
<td>{{class.name}}</td>
<td>
<span ng-repeat="subject in Subjects ">
<input type="checkbox" name="{{subject.name}}" ng-model="subject.model"
ng-change="Print(subject)">{{subject.name}}
</span>
</td>
</tr>
在上面的plunker中,当任何一个复选框被点击时,为什么都被选中?
演示 Plunker
Example2 的 HTML 很喜欢
<tr ng-repeat="class in Classes">
<td>{{class.name}}</td>
<td>
<span ng-repeat="subject in Subjects ">
<input type="checkbox" ng-model="Subject"
ng-change="Print(subject,class)">{{subject.name}}
</span>
</td>
</tr>
在上面的plunker中,当任何一个复选框被点击时,只有一个被选中?
Example1 和 Example2 的区别在于 ng-model 。在 Example1 中,$scope 中的默认值被分配给 ng-model,但在 Example2 中,新变量被分配了不是 $scope。我知道如果 ng-model 属性在作用域上尚不存在,它将被隐式创建并添加到作用域中。
为什么在 Example2 中可以选择单个复选框,而在 Example1 中不能选择?
【问题讨论】:
-
第一个变体
subject.name在所有行之间共享,因为您从 ng-repeat 分配变量,在第二个变体中您使用 每行唯一的任意值Subject变量。
标签: angularjs angularjs-scope angularjs-ng-repeat angularjs-ng-model