【问题标题】:select all checkboxes in angular 2?选择角度2中的所有复选框?
【发布时间】:2017-08-27 23:19:52
【问题描述】:

如果用户选择 Angular 中的“所有社区”复选框,我必须选择所有复选框。我尝试使用以下代码。但不工作。如何在角度 2 中解决此问题?

explore-list.Component.html

    <div class="checkbox_cont">
        <div class="checkbox ">
            <!--<input id="modal_checkbox1" type="checkbox" name="categories" unchecked="">-->
            <input type="checkbox" name="allneighbourhoods"  [value]="allneighbourhoods" (change)="neighbourhoodname [$event.target.getAttribute('value')]=$event.target.checked" id="allneighbourhoods" ng-click="toggleSelect($event)" />
            <label for="allneighbourhoods">All Neighbourhoods</label>
        </div>
    </div>
    <div class="modal_line"></div>                                                  
    <div class="checkbox " *ngFor="let neighbourhood of neighbourhoods;let i=index;">
        <input type="checkbox" name="neighbourhoodname[{{i}}]"  [value]="neighbourhood" (change)="neighbourhoodname [$event.target.getAttribute('value')]=$event.target.checked" id="{{neighbourhood}}"  [checked]='true' />
        <label for="{{neighbourhood}}">{{neighbourhood}}</label>
    </div> 

explore-list.Component.ts

    export class ExploreListComponent implements OnInit {  
        neighbourhoodname={};

        toggleSelect = function(event){       
            this.forEach(this.checkboxes, function(item){
                console.log(item);
                item.selected = event.target.checked;
            });
        }
    }

社区 json

【问题讨论】:

标签: angular


【解决方案1】:

您可以在复选框中添加ngModel

[(ngModel)]="neighbourhood.selected"

<form #f="ngForm" novalidate>
   <div class="checkbox_cont">
       <div class="checkbox ">
           <input type="checkbox" id="allneighbourhoods" name="allneighbourhoods"  value="allneighbourhoods" (click)="toggleSelect($event)" />
           <label for="allneighbourhoods">All Neighbourhoods</label>
       </div>
   </div>
   <div class="modal_line"></div> 
     <div class="checkbox " *ngFor="let neighbourhood of neighbourhoods;let i=index;">
      <input type="checkbox" name="neighbourhoodname[{{i}}]"  [checked]="neighbourhood.selected" value="neighbourhood.selected" id="{{neighbourhood.name}}" (change)="neighbourhood.selected = !(neighbourhood.selected)"/>
      <label for="{{neighbourhood.name}}">{{neighbourhood.name}}</label>
   </div>
   <input type="button" (click)="ApplyFilters(f.valid)"  class="btn btn-primary filters_btn" value="Apply Filters">
</form>

现在,你的功能将是一样的,

export class ExploreListComponent implements OnInit {  

 neighbourhoods = [{"name":"Taito"},{"name":"Shinjuku"},{"name":"Shibuya"}];

 toggleSelect = function(event){  

        this.allneighbourhoods = event.target.checked;
        this.neighbourhoods.forEach(function(item){
         console.log(item);
         item.selected = event.target.checked;
      });

  }       

  ApplyFilters(isValid: boolean) {      
    var datas  = this.neighbourhoods.filter(function (data) { return data.selected == true });
  console.log(datas);
    if (!isValid) return;         

 }
}

我添加了一个selected property,因此main checkboxselected value 将成为所有复选框中的model value

Here is a Working DEMO

【讨论】:

  • 我收到此错误。类 ExploreListComponent - 内联模板:100:15 原因:this.forEach 不是函数
  • 复选框邻域是动态的。
  • 我仍然遇到错误。类 ExploreListComponent - 内联模板:100:15 原因:无法读取未定义的属性“forEach”
  • neighbourhoods的邻域;让 i=index;">。在这个脚本中neighbourhoods 是动态的。
  • 你的组件上没有neighbourhoods?,查看我添加的DEMO,你会得到一个Idea
猜你喜欢
相关资源
最近更新 更多
热门标签