我希望当用户选中复选框时,该值为“ l.code”而不是布尔值
这是我的代码
<div class="form-group">
<label>Critères : </label>
<div class="checkbox-inline" *ngFor="let l of lesCriteres; let i= index">
<label>
<input type="checkbox" [value]="l.code" [(ngModel)]="actif.lesCriteresActifs[i].critere.code">{{l.code}}
</label>
</div>
</div>
但是它不起作用!当我检查时,它给出的是“ true”而不是“ l.code”。谢谢!
答案 0 :(得分:2)
您可以将“更改”事件处理程序与复选框上的事件绑定一起使用。
scanf
<form>
<div *ngFor="let l of lesCriteres">
<input type="checkbox" value="l.code" (change)="onChangeEvent($event, l.code)"> {{l.code}}<br>
</div>
</form>
答案 1 :(得分:0)
您可以执行以下操作:
HTML:
<div *ngFor="let data of emails">
<input type="checkbox" [value]="data.email" (change)="onChange(data.email, $event.target.checked)"> {{data.email}}<br>
</div>
ts:
import { Component } from '@angular/core';
import { FormGroup, FormBuilder, FormArray, FormControl } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular 6';
emailFormArray = [];
emails = [{ email: "email1" }, { email: "email2" }, { email: "email3" }, { email: 'email4' }]
myForm: FormGroup;
constructor(private fb: FormBuilder) { }
ngOnInit() {
this.myForm = this.fb.group({
useremail: this.fb.array([])
});
}
onChange(email: string, isChecked: boolean) {
if (isChecked) {
this.emailFormArray.push(email);
} else {
let index = this.emailFormArray.findIndex(x =>{
console.log(x);
return x == email
});
console.log(index)
this.emailFormArray.splice(index,1);
}
console.log(this.emailFormArray)
}
}