当我输入一个时,我有几个文本框会填充
I have a few text boxes that fill when I type in one
我正在创建一个动态表单,允许用户指定问题的内容以及存储在 Google Firestore 集合中的问题数量。当我填写一个文本框时,其他文本框就会充满完全相同的内容。
html 文件:创建表单并使用 angular 表单模块。
添加项目
<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 = "";
}
}
}}
预计文本框会单独填写,我可以记录他们的数据。
实际情况是所有文本框同时被填充。
您可以通过以下方式从文本框中获取单独的值
<ul *ngFor="let question of questions">
<li><strong>{{question.name}}</strong>
<input type="text" placeholder="Add Title" [(ngModel)] = 'question.answer' name = "title">
</li>
<ul *ngFor="let question of questions"><li><strong>{{question.name}}</strong><input type="text" placeholder="Add Title" [(ngModel)] = 'question.answer' name =title[]></li>
我正在创建一个动态表单,允许用户指定问题的内容以及存储在 Google Firestore 集合中的问题数量。当我填写一个文本框时,其他文本框就会充满完全相同的内容。
html 文件:创建表单并使用 angular 表单模块。 添加项目
<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 = "";
}
}
}}
预计文本框会单独填写,我可以记录他们的数据。
实际情况是所有文本框同时被填充。
您可以通过以下方式从文本框中获取单独的值
<ul *ngFor="let question of questions">
<li><strong>{{question.name}}</strong>
<input type="text" placeholder="Add Title" [(ngModel)] = 'question.answer' name = "title">
</li>
<ul *ngFor="let question of questions"><li><strong>{{question.name}}</strong><input type="text" placeholder="Add Title" [(ngModel)] = 'question.answer' name =title[]></li>