简体   繁体   English

angular material mat-checkbox绑定问题

[英]angular material mat-checkbox binding problem

I am using angular material for checkbox.我正在为复选框使用 angular 材料。

in component.ts在组件.ts

dataList: any[] = [
    { name: 'EmployeeName', flag: true, 'label': 'Employee Name'},
    { name: 'EmployeeDepartment', flag: false, 'label': 'Employee Department'},
    { name: 'EmployeeExperience', flag: false, , 'label': 'Employee Experience'}
]; // will have 30+ element in array

html (this opens as modal) html(以模式打开)

<div *ngFor="let data of dataList">
 <section>
  <mat-checkbox [checked]="data.flag" (change)="onChange($event, data)">{{data.label}}</mat-checkbox>
 </section>
</div>
<div>
  <button (click)="onClose()">
    Close
  </button>
  <button(click)="onApply()">Apply</button>
</div>

some of the checkbox will be pre-checked, if checked than behind modal there we will be filter dropdown for checked object.一些复选框将被预先选中,如果选中而不是在模态后面,我们将筛选下拉列表以选中 object。

problem is if I check the object(eg- Employee Department), than behind the modal that object(Employee Department) will be added instantly and the change in UI will be visible.问题是,如果我检查对象(例如员工部门),那么对象(员工部门)将立即添加到模态后面,并且 UI 中的更改将是可见的。 what I want is add that filter object (user can select multiple object at a time) only after clicking apply button and changes in UI should be visible.我想要的是仅在单击“应用”按钮后添加过滤器 object(用户一次可以 select 多个 object)并且 UI 中的更改应该可见。 if I click on close that nothing should be added.如果我单击关闭,则不应添加任何内容。

html for behind modal page html 用于后面的模态页面

//<ngx-select-dropdown> whole config is not listed
<div class="grid-container">
    <ng-container *ngFor="let data  of dataList">
      <div class="grid-item" *ngIf="data.flag">
        <div>
          <p>{{data.name }}</p>
        </div>
        <ngx-select-dropdown [config]="config">
        </ngx-select-dropdown>
      </div>
    </ng-container>
 </div>

I tried keeping checkbox values in another object and set value only on click of apply but the problem persisted.我尝试将复选框值保留在另一个 object 中,并仅在单击应用时设置值,但问题仍然存在。 what should I do in onChange($event, data) and onApply() so that I can achieve the behavior I want.我应该在onChange($event, data)onApply()中做什么,以便实现我想要的行为。

Thanks in Advance提前致谢

You could simply save the checkbox values in another object and once the user hits the apply button then have your function for example onApply() that will assign the new values from the checkbox to your data您可以简单地将复选框值保存在另一个 object 中,一旦用户点击应用按钮,然后让您的 function 例如onApply()将复选框中的新值分配给您的数据

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

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