Angular2,Ionic2:如何使用ngModel为嵌套*NGM中的单选按钮进行双向绑定?

Angular2,Ionic2:如何使用ngModel为嵌套*NGM中的单选按钮进行双向绑定?,ionic2,angular-ngmodel,angular2-directives,radiobuttonlist,ngfor,Ionic2,Angular Ngmodel,Angular2 Directives,Radiobuttonlist,Ngfor,我的API是JSON格式的,格式如下 我想以问题和选择的形式显示它。我正在尝试使用单选按钮显示选项。但是我的ngModel不起作用。我想使用.ts文件中的ngModel访问所选选项的整个对象 这是我的ts代码 export class Quiz implements OnInit { testcontents: Array<any>=[]; choiceObj:any; constructor(public navCtrl: NavController, pub

我的API是JSON格式的,格式如下

我想以问题和选择的形式显示它。我正在尝试使用单选按钮显示选项。但是我的
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);
    }
导出类测试实现OnInit{
testcontents:数组
我不明白为什么obj没有定义。

我的ngModel有问题。我不知道我应该把我的ngModel作为什么,我应该如何访问typescript文件中的“choiceObj”。有人能帮我一下吗。

该模型是choiceObj.choice,它在选择对象中不存在。 您需要将其替换为“choiceObj.choiceId”

<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>

{{i+1}}{{qContent.questionText}
{{choiceObj.choiceText}

模型为choiceObj.choice,该模型在选择对象中不存在。 您需要将其替换为“choiceObj.choiceId”

<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>

{{i+1}}{{qContent.questionText}
{{choiceObj.choiceText}

如果有帮助,请您将其标记为答案,好吗?非常感谢。它很有效。您能看看这个吗?如果您能将其标记为正确答案,我将不胜感激:-)。我现在就看一看。如果有帮助,请您将其标记为答案,好吗?非常感谢。它很有效。您能看看这个吗我可以把它标记为正确答案:-)。我现在来看看。