简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM