[英]Primeng checkbox checked issue angular
在我處理 angular 8 應用程序時,我正在使用primeng復選框並且我遇到了一些問題。復選框數據來自db。 因此,如果 status==0,我必須取消選中它,如果 status==1,即使加載表(默認)也應該選擇它。 我嘗試了很多方法,但它不起作用。 任何人都可以指出我的問題或任何建議都會有很大幫助。
onStatusChange(id: number,status: number) { this.id = id; console.log('Id', id); console.log('Checked'); this.status = status; console.log('Status', status); }
<p-checkbox [value]="text.id" [(ngModel)]="status" binary="true" (onChange)="onStatusChange(TextID,status)"></p-checkbox>
{"Text": [ {"ID":"1","Name":"Text","textContent":"content1,"status":"1"}, {"ID":"2","Name":"Text","textContent":"content1,"status":"1"}, {"ID":"3","Name":"Text","textContent":"content1,"status":"0"} ] }
text.IsActive[text.IsActive==1?'true':'false'
我在 onStatusChange 中獲取所需的數據,以使其處於活動或不活動狀態以將其更新到數據庫。 但問題在於在加載數據時根據選擇檢查復選框。 請幫助我..在過去的幾天里卡在這里..
我這個你可以使用(ngModelChange)
而不是(onChange)
。 代碼如下=>
HTML
<div>
<p-checkbox
label="My CheckBox"
binary="true"
[(ngModel)]="showLinkedRisksOnly"
(ngModelChange) ="myModelChanged($event)"
>
</p-checkbox>
</div>
<div>
Value: {{showLinkedRisksOnly}}
</div>
TS:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
showLinkedRisksOnly = true;
myModelChanged($event){
console.log($event);
}
}
注意:您可以檢查STACKBLITZ中的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.