如何获取在angular

时间:2018-12-02 11:17:25

标签: html angular input viewchild

我根据编号创建了一个“问题”对象的空哈希图。上一页中给出的问题集。然后,我在页面初始化时在哈希图中渲染“问题”对象。当用户单击“添加纸张”按钮时,我想获取每个问题对象给出的输入值。调用'test()'函数。执行此操作的最佳方法是什么?我当时在考虑使用viewChild来获取值,但它返回的是undefined。

组件文件

import { Component, OnInit, ViewChild,ElementRef, ViewChildren } from '@angular/core';
import { SharedserviceService } from "../services/sharedservice.service";

@Component({
  selector: 'app-admin-paper',
  templateUrl: './admin-paper.component.html',
  styleUrls: ['./admin-paper.component.css']
})
export class AdminPaperComponent implements OnInit {

  public paperID:any;
  public noOfQuestions : number ;
  public message : any;
  public i : number ;
  public questionSet = {};
  public question0 : any;

  @ViewChildren('testchild') testchild :ElementRef;
  constructor(private shared : SharedserviceService) { }

  ngOnInit() {
    this.shared.currentMessage.subscribe((message)=>{
      this.message = message;
    });
    this.paperID = this.message.paperID;
    this.noOfQuestions = this.message.noOfQuestions;

    for (this.i =0;this.i < this.noOfQuestions;this.i++){
      const question = {
        'paperID': this.paperID,
        'questionID': this.i,
        'question': '',
        'answers': [],
        'correctAnswer':''
      };
      this.questionSet[this.i] = question;
    }

    console.log(this.questionSet);
  }

  test(){

    //console.log(this.testchild.nativeElement.value);

    for (this.i =0;this.i < this.noOfQuestions;this.i++){

      const question = {
        'paperID': this.paperID,
        'questionID': this.i,
        'question': `question${this.i}');`,
        'answers': [`question${this.i}answer0`,`question${this.i}answer1`,`question${this.i}answer2`,`question${this.i}answer3`],
        'correctAnswer':'vghvhg'
      };
      console.log(question);
      this.questionSet[this.i] = question;
    }
    console.log("clicked");
    console.log(this.questionSet);
  }

}

HTML文件

<ng-container *ngIf="questionSet" >
<div class="panel panel-widget border-right" *ngFor="let question of questionSet | keyvalue; let i= index;" style="text-align: left; background-color: white; padding: 10px; border-radius: 10px;border-style: solid; border-width: 3px; border-color: #1aa3ff;">
  <div class="container">
    <div class="row">
    <div class="form-group col-md-10">
      <label> Question  {{question.value.questionID+1}}</label>
      <input type="text" class="form-control" #question{{i}} aria-describedby="emailHelp" placeholder="Enter question Here">
    </div>
  </div>
    <div class= "row">
    <div class="col-sm-2">
      <div class="form-group">
        <label>Answer 1</label>
        <input type="email" class="form-control" id="question{{i}}answer0" aria-describedby="emailHelp" placeholder="Answer 1">
      </div>
    </div>

    <div class="col-sm-2">
      <div class="form-group">
        <label>Answer 2</label>
        <input type="email" class="form-control" id="question{{i}}answer1" aria-describedby="emailHelp" placeholder="Answer 2">
      </div>
    </div>

    <div class="col-sm-2">
      <div class="form-group">
        <label>Answer 3</label>
        <input type="email" class="form-control"  id="question{{i}}answer2" aria-describedby="emailHelp" placeholder="Answer 3">
      </div>
    </div>

    <div class="col-sm-2">
      <div class="form-group">
        <label>Answer 4</label>
        <input type="email" class="form-control"  id="question{{i}}answer3" aria-describedby="emailHelp" placeholder="Answer 4">
      </div>
    </div>
  </div>

    </div>

    <div class="row">
      <div class="col-sm-6">
          <div class="form-group">
            <label>Correct Answer</label>
            <input type="text" class="form-control"  aria-describedby="emailHelp" placeholder="Correct Answer">
          </div>
        </div>
      <div class="col-sm-6">
          <button type="submit" style="margin-top:6%"  (click)="addQuestion($event,question,'new')" class="btn btn-primary mb-2">Add Question</button>
      </div>
    </div>
    <br><br>


</div>
</ng-container>

<button type="button" class="btn btn-primary btn-lg btn-block" (click)="test()">Add Paper</button>

1 个答案:

答案 0 :(得分:0)

*ngIf="questionSet"始终为true,因为在js中,空对象是真实值。像这样转换您的ngOnInit

  ngOnInit() {
    this.shared.currentMessage.subscribe((message)=>{
        this.message = message;
        this.paperID = this.message.paperID;
        this.noOfQuestions = this.message.noOfQuestions;

        for (let i =0; i < this.noOfQuestions; i++){
          const question = {
            'paperID': this.paperID,
            'questionID': this.i,
            'question': '',
            'answers': [],
            'correctAnswer':''
          };
          this.questionSet[this.i] = question;
        }

        console.log(this.questionSet);
    });
  }