简体   繁体   English

当我输入一个文本框时,会填满一些文本框

[英]I have a few text boxes that fill when I type in one

I am creating a dynamic form that allows the users to specify what the questions can be and the amount of them which are being stored in a Google Firestore collection. 我正在创建一个动态表单,允许用户指定问题的范围以及存储在Google Firestore集合中的问题数量。 When I fill put one text box the others become filled with the exact same thing. 当我填充一个文本框时,其他文本框将被完全相同的东西填充。

html file: This creates the forms and uses the angular forms module. html文件:这将创建表格并使用角度表格模块。 Add Item 添加项目

  <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 file: The ts file gets data from a data service and gives tha to the html. 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 = "";


    }


  }

}}

Expected that the text boxes would fill separately and I could record their data. 预计文本框将单独填充,我可以记录其数据。

Actual is that all text boxes get filled at the same time. 实际情况是所有文本框都同时填充。

The following way you can get the separate value from textboxes 您可以通过以下方式从文本框中获取单独的值

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

相关问题 我有2个文本框和一个按钮! 当我单击按钮时,它应该在所选文本框中写1,但是我的代码不起作用 - 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 am mapping items in react, each item has a text are underneath for a comment, when I type in one box the text appears in all the boxes 我有一个循环,其中文本框将动态生成。 我想捕获更改后的文本框值 - I have a loop where text boxes will generate dynamically. i want to capture the text box value when it changed 如何在一页上有多个选项卡式框 - how can I have multiple tabbed boxes on one page 当所有 select 框都有一个名称时,我如何从 DOM 添加/删除 select 框,以便在提交时我从活动的 select 框中获取值? - How can I add/remove select boxes from DOM when all select boxes have one name so that upon Submit I get value from active select box? 如何在容器中放置两个div,以便在调整另一个div的大小时自动调整其大小以填充容器? - How can I have two divs inside a container such that when resizing one the other automatically resizes to fill the container? 我有一个<p>为空的元素,在执行期间用文本填充元素时,如何给它高度而不将元素向下推? - I have a <p> element that is empty, how do I give it height and not push elements down when I fill the element with text during execution? 当我进行焦点时,HTML,显示每个文本框的ID - HTML , Display IDs of every Text boxes , when I make focus 使用jQuery,如果单击一个表单,如何清除表单上的所有文本框? - Using jquery how can I clear all the text boxes on a form if I click in one? 如何让一个选择选项影响另一个选择框可能的答案和价格? - How can I have one selection option affect another selection boxes possible answers & price?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM