【问题标题】:Using ng-checked on checkbox in nested ng-repeat is always true在嵌套的 ng-repeat 中使用 ng-checked 复选框始终为真
【发布时间】:2015-06-26 10:33:26
【问题描述】:

我正在使用嵌套的 ng-repeats 在页面上显示内容。嵌套的 ng-repeat 显示一个复选框,该复选框的选中状态应设置为 ng-repeat 中数据的布尔值。我正在使用 ng-checked 并传入一个真实值,但是,无论值如何,复选框总是被选中。

我已将我的代码简化为一个演示 jsFiddle 的行为。

HTML

<div ng-app="myApp">
    <div ng-controller="myController">
        <div ng-repeat="item in myArray">
            <h3>{{ $index }}.&nbsp;{{ item.title }}</h3>
            <div ng-repeat="child in item.subArray">
                <h4>({{ child.id }}). {{child.title}} </h4>
                <input type="checkbox" ng-checked="child.result" />
                <h6>Answer is: {{ child.result }}</h6>
            </div>
        </div>
    </div>
</div>

JavaScript

var myApp = angular.module('myApp', []);

function myController($scope) {
   $scope.myArray = [
            { id: "1", title: "my array 1", subArray: [
                { id: "1", title: "my sub array 1", result: "false" },
                { id: "1", title: "my sub array 2", result: "true" },
                { id: "1", title: "my sub array 3", result: "false" },
                { id: "1", title: "my sub array 4", result: "true" },
                { id: "1", title: "my sub array 5", result: "false" }]
            },
            { id: "1", title: "my array 2", subArray: [
                  { id: "1", title: "my sub array 1", result: "true" },
                  { id: "1", title: "my sub array 2", result: "false" },
                  { id: "1", title: "my sub array 3", result: "true" },
                  { id: "1", title: "my sub array 4", result: "false" },
                  { id: "1", title: "my sub array 5", result: "true" }]
            },
            {
                id: "1", title: "my array 3", subArray: [
                    { id: "1", title: "my sub array 1", result: "false" },
                    { id: "1", title: "my sub array 2", result: "false" },
                    { id: "1", title: "my sub array 3", result: "true" },
                    { id: "1", title: "my sub array 4", result: "false" },
                    { id: "1", title: "my sub array 5", result: "false" }]
            },
            {
                id: "1", title: "my array 4", subArray: [
                    { id: "1", title: "my sub array 1", result: "true" },
                    { id: "1", title: "my sub array 2", result: "false" },
                    { id: "1", title: "my sub array 3", result: "false" },
                    { id: "1", title: "my sub array 4", result: "false" },
                    { id: "1", title: "my sub array 5", result: "true" }]
            },
            {
                id: "1", title: "my array 5", subArray: [
                    { id: "1", title: "my sub array 1", result: "true" },
                    { id: "1", title: "my sub array 2", result: "true" },
                    { id: "1", title: "my sub array 3", result: "false" },
                    { id: "1", title: "my sub array 4", result: "true" },
                    { id: "1", title: "my sub array 5", result: "true" }]
            }
        ];
}

有人可以解释为什么会发生这种行为并帮助我了解如何纠正它吗?

【问题讨论】:

    标签: javascript jquery angularjs checkbox


    【解决方案1】:

    因为child.resultstring 而不是boolean,所以条件总是true

    使用以下:

    ng-checked="child.result == 'true'"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      • 2015-01-24
      • 2017-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      相关资源
      最近更新 更多