【发布时间】:2016-12-14 01:49:59
【问题描述】:
我有一个奇怪的场景,我的复选框没有使用 Angular (1.5) 勾选。我已经检查了 chrome Batarang,它看起来还可以,但是单击我的链接时我的复选框没有被勾选!?
我有一个使用 ng-repeat 显示的数组。在 ng-repeat 中,我有一个复选框和一个超链接,我想用它来控制复选框。
谁能看到为什么超链接没有选中复选框?
非常感谢,
标记
<tr ng-repeat="o in vm.myArray track by o.Id">
<td>
<!-- This works (changes the checkbox to checked)-->
<a href ng-click="o.Highlight=true">Just to test it (this works)</a>
<input type="checkbox" ng-model="o.Highlight" ng-click="vm.highlight(vm.myArray.Days, o.Name);" style="width: 15px" />
<!-- This fails (I'm expecting it to check the checkbox) -->
<a href ng-click="vm.highlight(vm.myArray.Days, o.Name)">
{{o.Name}}
</a>
</td>
</tr>
这个方法所做的只是遍历一个对象数组,这些对象具有属性“Highlight”(布尔值),然后用于控制 UI 中的内容。我希望 Highlight 属性控制复选框是否被选中。
观察这个方法在 Chrome batarang 中的作用,Highlight 属性设置正确,这反过来应该切换复选框,但由于某种原因它不是。
控制器中的高亮显示方法
(function() {
"use strict";
angular.module("bla").controller("homeCtrl", ["stuff", function (stuff) {
var vm = this;
vm.highlight = highlight;
// Iterate through array and if the fieldToHighlight matches the item.Name then toggle the item as highlighted.
function highlight(collection, fieldToHighlight) {
for (var i = 0; i < collection.length; i++) {
var element = collection[i];
if (element.Items != null) {
for (var counter = 0; counter < element.Items.length; counter++) {
var item = element.Items[counter];
if (fieldToHighlight == item.Name) {
if (item.Highlight) {
item.Highlight = false;
}
else {
item.Highlight = true;
}
}
}
}
}
}
cmets 后更新
Chrome 的 Angular 插件
这表明 Highlight 属性设置为 false onload。当我单击测试超链接时,Highlight 属性正确更改为 true。
angular plugin for chrome showing property correct data type
更新 2 - 显示 chrome 调试正在正确设置值
这个调试点是当我点击失败的超链接时。
code being debugged showing the highlight is working even though the ui is now updating
cmets 后更新(2016 年 12 月 13 日)
代码中的注释
<tr ng-repeat="o in vm.myArray track by o.Id">
<td>
<!-- checkbox -->
{{o.Highlight}}
<!-- clicking this changes the echo'ed variable immediately above but doesn't toggle the UI behaviour that relies on o.Highlight -->
<input type="checkbox" ng-click="vm.highlight(o);" ng-model="o.Highlight" />
<!-- name -->
<!-- clicking this toggles the right behaviour in the UI which relies on the o.Highlight property but it doesn't toggle the checkbox to be checked or unchecked -->
<a href ng-click="vm.highlight(o)">{{o.Name}}</a>
</td>
</tr>
【问题讨论】:
-
要检查您的复选框,您需要尝试使用 $scope,例如 $scope.Items[0].Highlight=true;
-
好的,但是为什么测试链接会在高亮功能内工作,并且 item.Highlight 工作正常。
-
“条件”变量从何而来?它似乎超出了范围......
-
为了实用地检查它,你需要设置喜欢它。所以 $scope 可以找到它。
-
@GautamPatadiya 我正在使用 controllerAs: 'vm' 所以没有“$scope”
标签: javascript angularjs