Html 如何在angular 7中单击按钮时取消选中复选框
我有几个复选框,它们的值来自循环,这里我的要求是只有onload所有复选框默认情况下会被选中,但是单击clear按钮所有复选框都应该被取消选中,下面是代码 home.component.html 试着这样做: .html 如果无法更改json,请执行以下操作: .ts .htmlHtml 如何在angular 7中单击按钮时取消选中复选框,html,angular,typescript,Html,Angular,Typescript,我有几个复选框,它们的值来自循环,这里我的要求是只有onload所有复选框默认情况下会被选中,但是单击clear按钮所有复选框都应该被取消选中,下面是代码 home.component.html 试着这样做: .html 如果无法更改json,请执行以下操作: .ts .html {{child.name} 感谢您的回答,我们不能在这里更改json,也不能在json中添加“checked:true”。不过,这是一种“正确”的方法。为什么不能添加选中的键?它们不必存在于“原始”json中,但
{{child.name}
感谢您的回答,我们不能在这里更改json,也不能在json中添加“checked:true”。不过,这是一种“正确”的方法。为什么不能添加选中的键?它们不必存在于“原始”json中,但您可能可以复制一份?检查修改后的演示您是否尝试过修改后的解决方案?
<li *ngFor="let child of nestedjson; let i = index"><input type="checkbox" checked>{{child.name}}</li>
<div><button (click)="clear()" type="submit">clear</button></div>
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import Speech from 'speak-tts';
import { RxSpeechRecognitionService, resultList, } from '@kamiazya/ngx-speech-recognition';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css'],
providers: [ RxSpeechRecognitionService ]
})
export class HomeComponent implements OnInit {
showit:any;
nestedjson:any;
constructor(private formBuilder: FormBuilder,public service: RxSpeechRecognitionService) {
}
ngOnInit() {
this.nestedjson = [
{ name: "parent1", value: ["child11", "child12"] },
{ name: "parent2", value: ["child2"] },
{ name: "parent3", value: ["child3"] }
];
this.showit = true;
}
clear(){
}
}
<li *ngFor="let child of nestedjson; let i = index">
<input type="checkbox" [(ngModel)]="child.checked">
{{child.name}}
</li>
<div><button (click)="clear()" type="submit">clear</button></div>
nestedjson = [
{ name: "parent1", value: ["child11", "child12"], checked: true },
{ name: "parent2", value: ["child2"], checked: true },
{ name: "parent3", value: ["child3"], checked: true }
];
clear() {
this.nestedjson.forEach(child => {
child.checked = false
})
}
checkedItems = this.nestedjson.map(x => ({ name: x.name, checked: true }));
<li *ngFor="let child of nestedjson; let i = index">
<input type="checkbox" [(ngModel)]="checkedItems[i].checked">
{{child.name}}
</li>