我有一个组件,我将用作加载多项选择题的外壳。到目前为止,组件的设置方式如下
零件
import { Component } from '@angular/core';
export class Answers{
id: string;
answer: string;
}
const answers: Answers[] = [
{
id: 'exp01q',
answer: 'Its fine as is.'
},
{
id: 'exp02q',
answer: 'I want to make minor adjustments.'
},
{
id: 'exp03q',
answer: 'I want to change my image'
},
{
id: 'exp04q',
answer: 'Ive never wanted to use a particular image until now.'
}
];
@Component({
moduleId: module.id,
selector: 'multi-radio-btn',
templateUrl: 'multi-rad-btn.component.html',
styleUrls: ['multi-rad-btn.component.css']
})
export class MultiRadioBtnShell {
question = 'How do you feel about your current image?';
id = 'exp-img-q';
name = 'exp-ques1';
ans = answers;
}
HTML 模板
<h3>radio button shell</h3>
<div class="row justify-content-center">
<fieldset [attr.id]='id' class="card col-8 justify-content-center">
<label class="ques-title">
{{question}}
</label>
<div class="row answer-row-section justify-content-center">
<div *ngFor="let answers of ans" class="col col-12 answer-row justify-content-center">
<div class="col justify-content-center">
<input type="radio"
[attr.id]="answers.id"
[attr.name]="name"
[attr.value]="answers.answer" hidden />
<label [attr.for]="answers.id" class="col ques-ans-title" style="background-color: #4b73a0;">
{{answers.answer}}
</label>
</div>
</div>
</div>
</fieldset>
</div>
现在这样设置的原因是因为我一开始尝试这样做的方式行不通,所以我去了英雄之旅教程来了解他们如何加载所有英雄。问题来自未定义的答案。因此,我以与他们对英雄相同的方式操纵该部分,只是为了做一些我能够遵循的事情,以确保我了解事物如何加载的机制。
我尝试这样做的原始方式是
// I had this right above the component
export class ExpQ{
question: string;
id: string;
name: string;
answers:[
{
id: string;
answer: string;
}
]
}
// I had this in the component's class
export const expq: ExpQ[] = [
{
question: 'How do you feel about your current image?',
id: 'exp-img-q',
name: 'exp-ques1',
answers:[
{
id: 'exp01q',
answer: 'Its fine as is.'
},
{
id: 'exp02q',
answer: 'I want to make minor adjustments.'
},
{
id: 'exp03q',
answer: 'I want to change my image'
},
{
id: 'exp04q',
answer: 'Ive never wanted to use a particular image until now.'
}
]
}
]
我在 html 中调用它
{{expq.question}}、{{expq.name}}、{{expq.answers.id}}、{{expq.answers.answer}} 等。
起初只是加载问题它工作正常,但当我到达answers:
它开始破坏的部分。我遇到了这个https://scotch.io/tutorials/using-angular-2s-model-driven-forms-with-formgroup-and-formcontrol并看到该addresses:
部分的语法与我需要的语法几乎相同构建我的数据。所以我把所有东西都重新制作成类似的样子。我仍然没有运气让它工作。
最终,我将通过父组件发送问题,@input
以及@output
我遇到的其他一些技巧。但在我考虑之前,我需要掌握如何将数据全部放入一个源中,以便正确读取嵌套的数据位。我遇到的所有示例都是简单的单层数据,所以我不确定我需要使用的语法。我怎样才能使这项工作?