加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在ng-repeat中检查AngularJS复选框

发布时间:2020-12-17 07:33:10 所属栏目:安全 来源:网络整理
导读:我知道解决方案不是很漂亮,但是我正在使用的模板需要它. 现在它可以正常工作,当产品在列表中时,它会向我显示“已添加”的span元素. section ng-repeat="product in products" label input type="checkbox" checklist-model="foobar.products" checklist-valu
我知道解决方案不是很漂亮,但是我正在使用的模板需要它.

现在它可以正常工作,当产品在列表中时,它会向我显示“已添加”的span元素.

<section ng-repeat="product in products">
    <label>
        <input type="checkbox" 
            checklist-model="foobar.products" 
            checklist-value="product.id">
        {{ product.name }}
    </label>

    <div ng-repeat="current_item in my.profile.items">
        <span ng-show="product.id==current_item.id">Added</span>
    </div>
</section>

我想要的是,当复选框后面还出现“已添加”文本时,选中要选中的复选框.

我试过这样做:

<ng-checked="{{my.profile.items.array.thing}}">

但那不起作用.因为数组中的产品如:

[{       
    id: 1,name:"Trinity",id: 2,name:"Van Gogh",id: 3,name:"Bonaqua",}]

而my.profile.items是一个包含更多信息的数组.因为它是我存储它的多对多关系.

有没有办法做到这一点?我不介意一个肮脏的解决方案:P

我试过这个:

// NG-check function
$scope.checkStoredValues = function(my) {

    // Loop trought my current stored items
    angular.forEach(my.profile.items,function(value,key) {

        // Loop trough the array    
        angular.forEach(value,key) {
            console.error(key);

            // If key == 'id'
            if(key == 'id'){
                // Push the value to the Array of the checkboxes,// so it's checked 
                // # Docs: http://vitalets.github.io/checklist-model/
                console.info(value); // Return's: integer 
                foobar.products.push(value); // Needs more then an integer I guess..

            }
        });
    });

};

返回:TypeError:无法读取undefined的属性’push’

将此功能添加到您的控制器:
$scope.isChecked = function(product) {
  var items = $scope.my.profile.items;
  for (var i=0; i < items.length; i++) {
    if (product.id == items[i].id)
      return true;
  }

  return false;
};

并为您的HTML使用

<section ng-repeat="product in products">
    <label>
        <input type="checkbox" 
               ng-checked="isChecked(product)">
        {{ product.name }}
    </label>

    <div ng-repeat="current_item in my.profile.items">
        <span ng-show="product.id==current_item.id">Added</span>
    </div>
</section>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读