Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 如何删除选中或全部复选框';角5中的s_Typescript_Angular5 - Fatal编程技术网

Typescript 如何删除选中或全部复选框';角5中的s

Typescript 如何删除选中或全部复选框';角5中的s,typescript,angular5,Typescript,Angular5,这是我的HTML,我有一个带有复选框和全局复选框的项目列表,用于选择所有复选框。单击delete(删除)后,我应该能够删除选中的或所有选中的复选框 <label (click)="delete()"> Delete these <button class="deletebutton">Delete</button></label> <ul> <input type="chec

这是我的HTML,我有一个带有复选框和全局复选框的项目列表,用于选择所有复选框。单击delete(删除)后,我应该能够删除选中的或所有选中的复选框

  <label (click)="delete()"> Delete these <button class="deletebutton">Delete</button></label>                 

  <ul>
      <input type="checkbox" (change)="usertodelete = $event.target.checked"/> // global checkbox to select all checkboxes

   <li *ngFor= "let user of users">
         <span>
             <input type="checkbox" [(ngModel)] ="usertodelete"/>
                   <label>{{user}}</label>
         </span>
       </li>
 </ul>
这是我的服务:

  remove(remove:any) {
    this.body = {
      "UserId":"abf"
     }
    return this.http.put('url'+remove, this.body,{headers :new HttpHeaders({'Content-Type':'application/json'})}
    ).subscribe((res:Response)=>{console.log(res)});
  }

您可以更改模型本身

简化:

假设您有一个由3项组成的数组,其中有一个值和“checked”的
c
属性:

HTML标记如下所示:

<p>
    <br/>

   <input type="checkbox"  *ngFor= "let item of data"   [(ngModel)]="item.c"/>
    <input  type="button" value="clear all" (click)="delete($event)"/>

<br/>
{{ data | json}}
</p>
  delete() {
    this.data.forEach(cb => cb.c = false)
  }
所以你基本上是在改变模型本身


您的哪些代码不起作用?(你尝试了什么?@FrankModica我编辑了我的代码并显示了我尝试过的内容。当我选中全局复选框时,它会选中所有复选框,但如果我在列表中选中一个复选框,它也会选中所有复选框。这就是问题所在,我应该能够通过选择全局复选框的
slice
获取索引(而不是对象)来删除单个或全部。此外,您将每个
ngModel
绑定到相同的
usertodelete
变量。您可能希望在每个用户对象上有一个
isChecked
属性,并绑定到该属性。
<p>
    <br/>

   <input type="checkbox"  *ngFor= "let item of data"   [(ngModel)]="item.c"/>
    <input  type="button" value="clear all" (click)="delete($event)"/>

<br/>
{{ data | json}}
</p>
  delete() {
    this.data.forEach(cb => cb.c = false)
  }