如何根据收到的值检查复选框
How to check checkbox base on Value received
了解
假设我收到值“是”和“否”。我的问题是我只想在收到“是”时选中复选框。
我试过的代码
<section class="pleft ptop" *ngFor="let item of patientPastMedicalHistoryList">
<mat-checkbox color="primary"
(change)="patientPastHistoryForm.get('substanceAbuseAlcohol').setValue(
$event.checked ? 'Yes' : 'No')"
checked="{{item.substanceAbuseAlcohol ==='Yes'}}"
>Alcohol</mat-checkbox><br>
</mat-form-field>
</section>
我试过了
这没有帮助。下面的代码始终选中复选框,即使收到的值为“NO”
[checked]="patientPastHistoryForm.get('substanceAbuseAlcohol').value=='Yes'"
checked="{{item.substanceAbuseAlcohol ==='Yes'}}"
形式
this.patientPastHistoryForm = new FormGroup({
patientId: new FormControl(this.clientId),
substanceAbuseAlcohol: new FormControl(''),})
数据
patientPastMedicalHistoryList: Array<PatientPastMedicalHistoryModel>=[];
这就是我获取值的方式
getPatientPastHistoryList() {
debugger
this.clientsService.getPatientPastHistoryList(this.clientId)
.subscribe(
(response: ResponseModel) => {
if (response.statusCode === 200) {
this.patientPastMedicalHistoryList = response.data;
} else {
this.patientPastMedicalHistoryList = [] ;
}
this.patientPastHistoryForm.patchValue({
...response.data,
})
});
}
收到数据
{
"expires_in": 0,
"data": [
{
"id": 0,
"patientId": 2143,
"substanceAbuseAlcohol": "No",
"substanceAbuseMarijuana": "Yes",
}
Stackblitz
response.data
是一个数组
你想要this.patientPastHistoryForm.patchValue({ ...response.data[0], })
了解 假设我收到值“是”和“否”。我的问题是我只想在收到“是”时选中复选框。
我试过的代码
<section class="pleft ptop" *ngFor="let item of patientPastMedicalHistoryList">
<mat-checkbox color="primary"
(change)="patientPastHistoryForm.get('substanceAbuseAlcohol').setValue(
$event.checked ? 'Yes' : 'No')"
checked="{{item.substanceAbuseAlcohol ==='Yes'}}"
>Alcohol</mat-checkbox><br>
</mat-form-field>
</section>
我试过了 这没有帮助。下面的代码始终选中复选框,即使收到的值为“NO”
[checked]="patientPastHistoryForm.get('substanceAbuseAlcohol').value=='Yes'"
checked="{{item.substanceAbuseAlcohol ==='Yes'}}"
形式
this.patientPastHistoryForm = new FormGroup({
patientId: new FormControl(this.clientId),
substanceAbuseAlcohol: new FormControl(''),})
数据
patientPastMedicalHistoryList: Array<PatientPastMedicalHistoryModel>=[];
这就是我获取值的方式
getPatientPastHistoryList() {
debugger
this.clientsService.getPatientPastHistoryList(this.clientId)
.subscribe(
(response: ResponseModel) => {
if (response.statusCode === 200) {
this.patientPastMedicalHistoryList = response.data;
} else {
this.patientPastMedicalHistoryList = [] ;
}
this.patientPastHistoryForm.patchValue({
...response.data,
})
});
}
收到数据
{
"expires_in": 0,
"data": [
{
"id": 0,
"patientId": 2143,
"substanceAbuseAlcohol": "No",
"substanceAbuseMarijuana": "Yes",
}
Stackblitz
response.data
是一个数组
你想要this.patientPastHistoryForm.patchValue({ ...response.data[0], })