在ng-repeat中检查AngularJS复选框

时间:2015-08-20 19:07:11

标签: angularjs checklist-model

我知道解决方案不是很漂亮,但是我正在使用的模板需要它。

现在它可以工作了,当产品在列表中时它会显示“已添加”的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}}">

但那不起作用。因为Array中的产品如:

[{       
    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, function(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: Cannot read property 'push' of undefined

4 个答案:

答案 0 :(得分:10)

将此功能添加到您的控制器:

$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>

答案 1 :(得分:4)

您可以做的另一个技巧是创建一个特定于您需要的视图模型。

例如,您在配置文件中拥有一系列产品和项目。

在控制器中,您可以执行以下操作:

 $scope.products.forEach(function(p){
       var items = $scope.my.profile.items;
       var wasAdded = false;
       for (var i=0; i < items.length; i++) {
           if (product.id == items[i].id)
                 wasAdded =  true;
                 i = items.length;
        }
       $scope.productsViewModels.push({product:p, added:wasAdded});
 });

既然您已根据需要在视图中创建了数据,那么您可以这么做:

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

     <span ng-show="p.added">Added</span>
 </section>

当我合并来自多个来源的数据时,我更喜欢在控制器中处理它并创建一个viewModel,这将使我的生活在视图中变得容易。

答案 2 :(得分:2)

您需要实现类似AngularJs ng-checked using a function

的内容

您只需要根据您的多对多产品数组实现自己的逻辑,并相应地返回true或false。

答案 3 :(得分:0)

在复选框

之后添加此项
ui-sref-active="active"

并删除<span ng-if="my.profile.items.indexOf(product) >= 0">added</span>