我有一个复选框,每个复选框都有一个name属性。如何简化基于www.example.com创建的ngIfevent.target.name?
<input [checked]="financeValue" (click)="saveSetting($event)" id="finance" aria-describedby="finance-description" name="finance" type="checkbox" class="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
<input [checked]="accountValue" (click)="saveSetting($event)" id="account" aria-describedby="account-description" name="account" type="checkbox" class="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
<input [checked]="technoValue" (click)="saveSetting($event)" id="techno" aria-describedby="techno-description" name="techno" type="checkbox" class="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
<input [checked]="marketingValue" (click)="saveSetting($event)" id="marketing" aria-describedby="marketing-description" name="marketing" type="checkbox" class="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
<input [checked]="salesValue" (click)="saveSetting($event)" id="sales" aria-describedby="sales-description" name="sales" type="checkbox" class="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
<input [checked]="logisticValue" (click)="saveSetting($event)" id="logistic" aria-describedby="logistic-description" name="logistic" type="checkbox" class="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
<input [checked]="callValue" (click)="saveSetting($event)" id="call" aria-describedby="call-description" name="call" type="checkbox" class="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
financeValue: any;
accountValue: any;
technoValue: any;
marketingValue: any;
salesValue: any;
logisticValue: any;
callValue: any;
ngOnInit(): void {
const getSession = sessionStorage.getItem('setting');
if (getSession) {
const parseSession = JSON.parse(getSession);
parseSession.forEach((item, index) => {
if(parseSession[index].setName === 'finance') {
this.financeValue = item.setValue;
} else if(parseSession[index].setName === 'account') {
this.accountValue = item.setValue;
} else if(parseSession[index].setName === 'techno') {
this.technoValue = item.setValue;
} else if(parseSession[index].setName === 'marketing') {
this.marketingValue = item.setValue;
} else if(parseSession[index].setName === 'sales') {
this.salesValue = item.setValue;
} else if(parseSession[index].setName === 'logistic') {
this.logisticValue = item.setValue;
} else if(parseSession[index].setName === 'call') {
this.callValue = item.setValue;
}
});
}
}
基于www.example.com简化ngIfevent.target.name
1条答案
按热度按时间plicqrtu1#
NgModel
在这里会很有帮助。测试是一个"设置"数组。在您的情况下:
然后使用
*ngFor
(可选)自动构建inputs
。每次更改都将被设置到"settings"数组中。这是双向绑定的(通过输入更改代码 * 或 * html都可以)。inputshtml中的**log()**方法只是记录数组,你不需要它。