简体   繁体   English

如何通过ng-checked和ng-model选择全部

[英]How to select all with ng-checked and ng-model

I have an Ionic application(it work the same like Angularjs) and I have a little problem. 我有一个Ionic应用程序(它的工作原理与Angularjs一样),但我有一个小问题。

<ion-list class="list-inset subcategory" ng-repeat="item in shops">
      <ion-checkbox class="item item-divider item-checkbox-right" ng-model="selectAll">
        {{item}}
      </ion-checkbox>
      <ion-item ng-repeat="value in data | filter:{shopName: item}" class="item-thumbnail-left" ng-click="openProduct(value)">
...
        <div class="row">
          <ion-checkbox stop-event='click' ng-model="value.selected" ng-checked="selectAll">{{value.name}}</ion-checkbox>
        </div>
...
    </ion-list>

When I click on item with ng-model="selectAll" all items is selected. 当我单击带有ng-model="selectAll"项目时,所有项目ng-model="selectAll"选中。 But I have property value.selected . 但是我有属性value.selected It sets false for each one value . 它将每个value设置为false。 When I click on item with ng-model="value.selected" it changes. 当我单击带有ng-model="value.selected"它会更改。 But when I want selec all and click on item with ng-model="selectAll" this propety doesn't change. 但是,当我要全部选择并单击带有ng-model="selectAll"此属性不变。 Help me please. 请帮帮我。

Note: I have ng-repeat in the ng-repeat. 注意:我在ng-repeat中有ng-repeat。 First ng-repeat is for shops, the second is for products. 第一个ng-repeat用于商店,第二个用于产品。 And I have a filter by shopName. 我有一个按shopName的过滤器。 And I want select all by shops. 我想按商店选择所有商品。 Now it works how I want, but doesn't change property value.selected . 现在它可以按我的方式工作,但不会更改属性value.selected Value it is produt, item it is shop. 重视产品,重视产品。

the state of selectAll can be derived from the state of your other check boxes so it should not be stored as a seperate field. selectAll的状态可以从其他复选框的状态派生,因此不应将其存储为单独的字段。

You could use a getterSetter on the selectAll model to determine weather it should be checked. 您可以在selectAll模型上使用getterSetter来确定应检查的天气。 eg 例如

<input type="checkbox" ng-model="selectAll" ng-model-options="{getterSetter: true}"/>

JS JS

var getAllSelected = function () {
    var selectedItems = $scope.Items.filter(function (item) {
        return item.Selected;
    });

    return selectedItems.length === $scope.Items.length;
}

var setAllSelected = function (value) {
    angular.forEach($scope.Items, function (item) {
        item.Selected = value;
    });
}

$scope.selectAll = function (value) {
    if (value !== undefined) {
        return setAllSelected(value);
    } else {
        return getAllSelected();
    }
}

http://jsfiddle.net/2jm6x4co/ http://jsfiddle.net/2jm6x4co/

you can use ngClick on the item with ng-model="selectAll" . 您可以通过ng-model="selectAll"在项目上使用ngClick you can call a function in ng-click and then make selected=true for all other items where ng-model="value.selected" . 您可以在ng-click中调用一个函数,然后对其中ng-model="value.selected"所有其他项设置selected = true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM