我正在创建一个动态表格,允许用户指定问题的范围以及存储在Google Firestore集合中的问题数量。当我填充一个文本框时,其他文本框将被完全相同的东西填充。
html文件:这将创建表单并使用角度表单模块。 新增项目
<ul *ngFor="let question of questions">
<li><strong>{{question.name}}</strong>
<input type="text" placeholder="Add Title" [(ngModel)] = 'answer.name' name = "title">
</li>
</ul>
<input type="submit" value = "Submit" class = "btn">
</form>
ts文件:ts文件从数据服务获取数据,并将tha提供给html。
@Component({
selector: 'app-add-item',
templateUrl: './add-item.component.html',
styleUrls: ['./add-item.component.scss']
})
export class AddItemComponent implements OnInit {
questions: Question[];
items: Item[];
answers: Answer[];
answer: Answer = {
name:'',
number:0
}
item: Item = {
title:'',
description:'',
}
question: Question = {
name:'',
number:null,
answer:'',
}
database;
doc:'';
constructor(private dataService: DataService, private route: ActivatedRoute) {
this.route.params.subscribe(params => this.doc = params["id"])
}
ngOnInit() {
this.dataService.getQuestions(this.doc).subscribe(questions => {
this.questions = questions;
})
}
onSubmit(){
{
if(this.answer.name != ''){
console.log(this.answer.name)
this.dataService.addAnswer(this.answer);
this.answer.name = "";
}
}
}}
预计文本框将分别填充,我可以记录其数据。
实际上是所有文本框都同时填充。
答案 0 :(得分:0)
通过以下方法可以从文本框中获取单独的值
<ul *ngFor="let question of questions">
<li><strong>{{question.name}}</strong>
<input type="text" placeholder="Add Title" [(ngModel)] = 'question.answer' name = "title">
</li>
答案 1 :(得分:0)
<ul *ngFor="let question of questions"><li><strong>{{question.name}}</strong><input type="text" placeholder="Add Title" [(ngModel)] = 'question.answer' name =title[]></li>