3 回答

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個(gè)贊
這是我認(rèn)為您想要的堆棧閃電戰(zhàn)?您需要為您的復(fù)選框創(chuàng)建一個(gè)數(shù)組
checkboxes = [
{
name: 'First checkbox',
value: false
},
{
name: 'Second checkbox',
value: false
},
]
使用 *ngFor 獲得一個(gè)通用 html,當(dāng)您向數(shù)組添加另一個(gè)復(fù)選框時(shí),您不需要編輯該 html
<app-checkbox *ngFor="let box of checkboxes; let i = index"
[checkboxName]="box.name"
[checkboxData]="box.value"
(toggle)="onRoleChangeCheckbox($event, i)"></app-checkbox>
然后您的onRoleChangeCheckbox()函數(shù)可以變得通用并使用索引更新您的數(shù)組
onRoleChangeCheckbox(ev, index) {
this.checkboxes[index].value = ev;
}
這種方法應(yīng)該可以減少很多重復(fù)的代碼,因?yàn)槟恍枰驍?shù)組添加新的復(fù)選框。

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊
可能這不是更好的方法,但它可能在某些情況下有效,在所有情況下都會(huì)有一個(gè)發(fā)射處理程序。
import { Component, OnInit, Input, Output, EventEmitter } from "@angular/core";
@Component({
selector: "app-checkbox",
templateUrl: "./checkbox.component.html",
styleUrls: ["./checkbox.component.css"]
})
export class CheckboxComponent implements OnInit {
@Input() checkboxName;
@Input() checkboxData:any;
@Input() checkBoxLinkedProp:any; // added another property it is the name of the property in the parent component you are updating
@Output() toggle: EventEmitter<any> = new EventEmitter<any>(); // emit a object instead of bool
constructor() {}
ngOnInit() {}
onToggle() {
const checkedOption = this.checkboxData;
this.toggle.emit({checked:checkedOption , checkBoxLinkedProp: this.checkBoxLinkedProp });
}
}
app.component.html [checkBoxLinkedProp] = "'checkbox1Value'" - 我將道具名稱(chēng)作為字符串傳遞給子復(fù)選框組件。并在切換時(shí)調(diào)用一個(gè)方法(toggle)="onRoleChangeCheckbox($event)"
this.toggle.emit將發(fā)射帶有我們傳遞的字符串道具的對(duì)象
<app-checkbox [checkboxName]='checkbox1' [checkboxData]='checkbox1Value'
[checkBoxLinkedProp] = "'checkbox1Value'"
(toggle)="onRoleChangeCheckbox($event)"></app-checkbox>
<app-checkbox [checkboxName]='checkbox2' [checkboxData]='checkbox2Value' (toggle)="onRoleChangeCheckbox($event)"
[checkBoxLinkedProp] = "'checkbox2Value'"
></app-checkbox>
app.component.ts onRoleChangeCheckbox({checkBoxLinkedProp , checked})這個(gè)方法將把我們作為字符串從發(fā)射事件傳遞過(guò)來(lái)的屬性名稱(chēng)設(shè)置為類(lèi)的狀態(tài)。
import { Component, VERSION } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
checkbox1 = 'First checkbox';
checkbox1Value = false;
checkbox2 = 'Second checkbox';
checkbox2Value = false;
onRoleChangeCheckbox({checkBoxLinkedProp , checked}) {
this[checkBoxLinkedProp] = checked; // property name that passed to child , is received in this emit event and as this will be set
console.log(this.checkbox1Value , checkBoxLinkedProp); // tested value for 1st checkbox
}
}

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
我很難準(zhǔn)確地說(shuō)出你想要什么,但如果你使用 eventEmitter & Output,你可以用一個(gè)單一的發(fā)射語(yǔ)句發(fā)射一個(gè)復(fù)雜的 json obj,并向父級(jí)發(fā)送盡可能多的數(shù)據(jù):
@Output() messageEvent = new EventEmitter<any>();
var obj = { check1: true, check2: false, check3: false }
this. messageEvent.emit(obj);
添加回答
舉報(bào)