【问题标题】:Angular - Check/Uncheck All CheckboxesAngular - 选中/取消选中所有复选框
【发布时间】:2020-12-12 20:13:42
【问题描述】:

我的工作是完成以下功能。我有 3 个复选框,选择一个后,我希望另外两个也被选中。

我使用现成的组件来创建复选框。

<form [formGroup]="data" name="name" >
  <div class="form-group">
    <div class="form__element">
      <nb-checkbox name="groupname" value="Check All" label="Check All" formControlName="isAgree" [(ngModel)]="myVar2" [ngModelOptions]="{standalone: true}"></nb-checkbox>
    </div>
  </div>

  <div class="features__box">
    <section class="agreements">
          <ul class="features__list">
        <li class="features__item" *ngFor="let agreement of data.agreements.value">
          <div class="form__element">
            <nb-checkbox name="groupname" value={{agreement.description}} label={{agreement.description}} [checked]="myVar2" (change)="myVar2 = !myVar2"></nb-checkbox>
          </div>
        </li>
      </ul>
    </section>
    <section class="statements">
          <ul class="features__list">
        <li class="features__item" *ngFor="let statement of data.statements.value">
          <div class="form__element">
            <nb-checkbox name="groupname" value={{statement.description}} label={{statement.description}} [checked]="myVar2" (change)="myVar2 = !myVar2"></nb-checkbox>
          </div>
        </li>
      </ul>
    </section>
  </div>
</form>

我在主复选框中添加了[(ngModel)] =" myVar2 "[ngModelOptions] =" {standalone: true} ",并添加了[checked] =" myVar2 "(change) =" myVar2 =! myVar2 to my next checkbox.file.component.ts 文件中我添加了myVar2: boolean = false;

但是,上述解决方案不起作用。我在控制台中收到以下错误

    
    ERROR in src/app/file.component.html:64:66 - error NG8002: Can't bind to 'ngModelOptions' since
it isn't a known property of 'nb-checkbox'.
    1. If 'nb-checkbox' is an Angular component and it has 'ngModelOptions' input, then verify that it is part of this module.
    2. If 'nb-checkbox' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
    3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.
    
    64                   formControlName="isAgree" [(ngModel)]="myVar2" [ngModelOptions]="{standalone: true}"></nb-checkbox>
                                                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
      src/app/file.component.ts:14:16
        14   templateUrl: './file.component.html',
                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        Error occurs in the template of component CeidgPositiveComponent.
    src/app/file.component.html:94:121 - error NG8002: Can't bind to 'checked' since it isn't a known property of 'nb-checkbox'.
    1. If 'nb-checkbox' is an Angular component and it has 'checked' input, then verify that it is part of this module.
    2. If 'nb-checkbox' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
    3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.
    
    94                     <nb-checkbox name="groupname" value={{statement.description}} label={{statement.description}} [checked]="myVar2" (change)="myVar2 = !myVar2"></nb-checkbox>
                                                                                                                               ~~~~~~~~~~~~~~~~~~
    
      src/app/file.component.ts:14:16
        14   templateUrl: './file.component.html',
                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        Error occurs in the template of component File.

我已经导入了模块import {FormsModule, ReactiveFormsModule} from '@ angular / forms'; import {NgxsModule} from '@ ngxs / store';

有人知道如何解决这个问题吗?

【问题讨论】:

  • 您需要导入NbCheckboxModule

标签: angular forms checkbox


【解决方案1】:

根据我们在 cmets 中的讨论更新我的答案,以选中/取消选中所有复选框。

component.ts

checklist

checkUncheckAll(evt) {
  this.checklist.forEach((c) => c.isSelected = evt.target.checked)
}

ngOnInit() {
this.checklist = [
    {id:1,value:'Elenor Anderson',isSelected:false},
    {id:2,value:'Caden Kunze',isSelected:false},
    {id:3,value:'Ms. Hortense Zulauf',isSelected:false},
    {id:4,value:'Grady Reichert',isSelected:false},
    {id:5,value:'Dejon Olson',isSelected:false},
    {id:6,value:'Jamir Pfannerstill',isSelected:false},
    {id:7,value:'Aracely Renner DVM',isSelected:false},
    {id:8,value:'Genoveva Luettgen',isSelected:false}
  ];
}

在您的模板中

<div class="container">
  <div class="text-center mt-5">
    <div class="row">
       <div class="col-md-6">
        <ul class="list-group">
            <li class="list-group-item">
              <input type="checkbox" name="list_name" value="m1" (change)="checkUncheckAll($event)"/> <strong>Select/ Unselect All</strong>
            </li>
          </ul>
          <ul class="list-group">
            <li class="list-group-item" *ngFor="let item of checklist">
              <input type="checkbox" [checked]="item.isSelected" name="list_name" value="{{item.id}}"/>
              {{item.value}}
            </li>
          </ul>
      </div>
   </div>
 </div>
</div>

工作DEMO

【讨论】:

  • 但是nb-checkbox是我的自定义组件,我没有使用你建议的ui库
  • 我明白了,您说您使用的是现成的组件并以nb- 前缀命名,所以我认为使用库,所以您使用自己的自定义代码对吗?
  • true,这就是我的复选框的样子``` ```
  • @Doe 您的确切要求是什么,您需要一个表格,其中列出了多个复选框和一个带有 Check All 标签的父复选框,您只需单击该父 Check All 复选框和所有其他复选框都应该被选中?
  • 我想在页面上有几个复选框,然后单击一个并全部选中。
猜你喜欢
  • 2021-03-22
  • 2011-06-15
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多