我使用以下格式在JSON中使用我的API。
我希望以问题和形式显示这一点。选择格式。我试图使用单选按钮显示选项。但我的ngModel
无效。我想在我的.ts文件中使用ngModel
访问所选选项的整个对象。
这是我的ts代码
export class Quiz implements OnInit {
testcontents: Array<any>=[];
choiceObj:any;
constructor(public navCtrl: NavController, public navParams: NavParams,public http:Http) {}
ngOnInit()
{
this.launchfunc();
}
launchfunc()
{
this.http.post('launchtest/getTestContent',this.launchtestobj).subscribe(resp=>{
this.testcontents = resp.data.questions;
});
}
selectChoice($event)
{
console.log("12312341234",event);
console.log("obj",this.choiceObj);
}
这是我的HTML代码
<ion-content padding>
<div *ngFor="let qContent of testcontents; let i = index">
{{i+1}} {{qContent.questionText}}
<div *ngFor="let choiceObj of qContent.choice">
<input name='options' type='radio' [value]='choiceObj' [(ngModel)]='choiceObj.choice' (ngModelChange)="selectChoice(choiceObj)" />
<label [for]='choiceObj'>{{choiceObj.choiceText}}</label>
</div>
</div>
这是上述代码的输出。 我不明白为什么obj未定义。 我对ngModel有疑问。我不明白我应该把我的ngModel放在哪里以及如何访问&#34; choiceObj&#34;在打字稿文件中。有人可以帮助我。
答案 0 :(得分:2)
模型是choiceObj.choice,它在选择对象中不存在。 你需要用&#39; choiceObj.choiceId&#39;
替换它<ion-content padding>
<div *ngFor="let qContent of testcontents; let i = index">
{{i+1}} {{qContent.questionText}}
<div *ngFor="let choiceObj of qContent.choice">
<input name='options' type='radio' [value]='choiceObj' [(ngModel)]='choiceObj.choiceId' (ngModelChange)="selectChoice(choiceObj)" />
<label [for]='choiceObj'>{{choiceObj.choiceText}}</label>
</div>
</div>