【问题标题】:ngModel checkbox not shown as selectedngModel 复选框未显示为选中状态
【发布时间】:2015-11-28 05:32:55
【问题描述】:

我无法让复选框显示模型的正确状态(选中/未选中)。我的控制器中有以下内容:

app.controller('PhotosCtrl', ['$scope', function($scope) {
    $scope.form = {};
    $scope.editPhotos = {
        token: $scope.token,
        idArray: $scope.idArray
    };
}]);

我的表单如下所示:

<form accept-charset="UTF-8" name="editPhotosForm" ng-submit="submit(editPhotos);" multipart="true" novalidate>
    <input name="utf8" type="hidden" value="✓">
    <input name="authenticity_token" type="hidden" ng-model="editPhotos.token" ng-init="editPhotos.token='<%= form_authenticity_token %>'">
    <div class="results-label"><b>{{total_count}}</b> <span ng-if="total_count == 1">Photo</span><span ng-if="total_count != 1">Photos</span> Found</div>
    <div>
        <div class="col">
            <a ng-repeat="r in results" class="card result-link">
                <div class="content result">
                    <div class="caption">
                        <input type="checkbox" ng-model="idArray[r.id]">
                    </div>

                    <div class="image-container" ng-style="{'background-image': 'url(' + r.image_url + ')'}">
                    </div>
                </div>
            </a>
        </div>
    </div>
</form>

在我的转发器元素上,我调用ng-init="idArray[r.id] = 'false'" 来为r 中的每个项目初始化一个键r.id = 'false'。我不知道我是否需要这样做。我已经尝试了没有它的代码,它没有任何区别。我也尝试过使用ng-value-trueng-value-false,但它们似乎对我不起作用。

我在这个问题上看到的大多数其他帖子都处理简单的变量(例如$scope.someVar = true,而不是像哈希这样的更复杂的结构。

这是idArray的结构:

$scope.idArray = {1290: "false", 1291: "true", 1292: "true", 1293: "false", 1294: "false", 1414: "false"};

这是由我的转发器中的ng-init 生成的,因为事先无法知道照片的 ID。

这是results 的样子:

{
    id: 1290,
    company_id: null,
    image_url: "http://s3.amazonaws.com/mybucket/photos/images/000/001/290/original/214.JPG?1432217895"
}

【问题讨论】:

  • 您预计会发生什么,而会发生什么?没有要迭代的results 数组,儿子,我看不到您如何看到任何复选框。而且您将字符串 'false' 与布尔值 false 混淆了。
  • 在我的转发器中,我将哈希键初始化为'false' 而不是false,因为ng-true-valueng-false-value 的值始终是字符串。 results 数组是通过 AJAX 调用在 $scope 上定义的,该调用返回数据库中的照片列表。我将idArray的结构添加到我上面的问题中以供参考。
  • 在阅读我的答案时,我发现我的答案可能存在问题,具体取决于您的对象 result 的数据结构。可以发个样品吗?
  • 我刚刚在我的问题上附加了一个 sn-p。
  • 你的结果是这个对象的数组?

标签: angularjs checkbox angular-ngmodel


【解决方案1】:

执行以下操作

    ng-init="idArray[r.id] = 'false'"

您正在将字符串值 'false' 分配给您的对象。

你不能在你的控制器中处理它吗?

$scope.idArray = {};
for (var i = 0; i < $scope.results.length; ++i){
    $scope.idArray[$scope.results[i].id] = (i%2 == 0);
}

并删除 ng-init=""(根据 Angular 文档,这是一种不好的做法 https://docs.angularjs.org/api/ng/directive/ngInit)。

另一件事是包装复选框元素的锚元素。这导致点击事件没有在复选框上触发,而只是在锚点上。

    <div class="col">
        <div ng-repeat="r in results" class="card result-link">
            <div class="content result">
                <div class="caption">
                    <input type="checkbox" ng-model="idArray[r.id]">
                </div>

                <div class="image-container" ng-style="{'background-image': 'url(' + r.image_url + ')'}">
                </div>
            </div>
        </div>
    </div>

小提琴:

http://jsfiddle.net/patxy/wak7pwwp/

【讨论】:

  • 所以我从中继器中删除了ng-init,现在我只有&lt;input type="checkbox" ng-model="idArray[r.id]"&gt;。这会生成一个看起来像 {1291: true} 的哈希,但复选框仍然没有显示正确的状态。它始终处于未选中状态。
  • 我做了一个小改动ng-model="$parent.idArray[r.id]"。你能测试一下吗? ng-repeat 为每个元素创建一个新范围。所以你的 ng-model 是在一个子范围内。
  • $parent.idArray[r.id] 不走运。我也尝试了$parent.editPhotos.idArray[r.id],但没有成功。如果我console.log($scope.idArray) 我得到了正确的数据结构,那么选中该框就是在做某事。
  • 我注意到,当我单击复选框时,id: true 会添加到 idArray 中。该复选框仍然未选中。如果我再次单击它,我希望从数组中删除键,或者更改值。两者都没有发生。
  • 您的复选框位于锚点中。也许这就是这个问题的原因。当您这样做时,并非每个浏览器的反应都相同。你能试试这个更新的版本吗? (新小提琴也)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 2016-12-04
  • 2018-02-13
  • 2018-12-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多