![](/img/trans.png)
[英]I have 2 text Boxes and one button! When I click on button it should write 1 in the chosen text box but my code is not working
[英]I have a few text boxes that fill when I type in one
我正在創建一個動態表單,允許用戶指定問題的范圍以及存儲在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 = "";
}
}
}}
預計文本框將單獨填充,我可以記錄其數據。
實際情況是所有文本框都同時填充。
您可以通過以下方式從文本框中獲取單獨的值
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.