如何获得角度2的chechkbox响应?

时间:2017-03-20 09:11:14

标签: angular checkbox angular2-forms

here is the quiz.component.html file.

选项值来自服务。     我想在这里使用复选框保存用户响应。当他点击保存按钮时。请帮助我?

<html>
<div class="jumbotron">
<div class="container">
   <div class="page-header">
         <h1>Quiz </h1>
          <h4> Solve Questions Here</h4>
  </div>
       <div *ngFor="let question of questions; let i = index">

               <div class="panel panel-info">
                        <div class="panel-heading">
                              <h3 class="panel-title">{{question.category}} Quiz</h3>
                         </div>
                       <div class="panel-body">
                               <p>    
                                    <span> {{i+1}}.</span> &nbsp; {{question.ques}}
                               </p>
           <form [(ngSubmit)]=saveResponse() >                    
           <ol type="a">
               <li> <input type="checkbox" #op1 value={{question.op1}} >&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;{{question.op1}}</li>
               <li><input type="checkbox" #op2  value={{question.op2}} >&nbsp; &nbsp;&nbsp;    &nbsp;{{question.op2}}</li>
               <li><input type="checkbox" #op3  value={{question.op3}} >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{question.op3}}</li>
               <li><input type="checkbox" #op4  value={{question.op4}} >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{question.op4}}</li>
           </ol>

           <ul class="pager">
                  <li><button type="button"> Prev</button></li>
                  <li><button type="button" > Save </button></li>
                  <li><button type="button" > Next</button></li>
           </ul>
       </form>
         </div>
        <br>
        <br>

        </div>

    </div>
</div>
</div>
</html>

这是quiz.component.ts文件...... 在这里,我尝试构建一个函数,该函数采用复选框值,但在选中复选框时无法正确获取值。

 import { Component, OnInit } from '@angular/core';
    import { QuizService } from '../../services/quiz.service'
    import { ActivatedRoute,Router } from '@angular/router';
    import 'rxjs/add/operator/switchMap';
    @Component({
      selector: 'quiz',
      templateUrl: 'quiz.component.html',
    })

    export class QuizComponent implements OnInit {
      questions: String[];
     // submitChoices:{}
    //  optValue:String[];

    // pager = {
    //     index: 0,
    //     size: 1,
    //     count: 1
    //   };

      constructor(private _quizService: QuizService,private route:ActivatedRoute) {

      }

      ngOnInit() {
        // this._quizService.getQues().subscribe(ques => {
        //   this.questions = ques;
        // //  console.log(ques);
        // });

    this.route.params.switchMap((params) => this._quizService.getQuesByCategory(params['quizType'])).subscribe(q  => {
      this.questions= q;
    });

      }


    // get filteredQuestions(){
    // return (this.questions) ? 
    // this.questions.slice(this.pager.index,this.pager.index+this.pager.size):[];
    // }



    //   getChoices(value:any,event){

    // // if(event.target.checked==true)
    // // {
    // // this.optValue.push(value);
    // // }
    // // console.log(this.optValue);
    // }
    }

1 个答案:

答案 0 :(得分:1)

最简单的方法通常是使用function myFunction() { for (i = 0; i < 5000;) { setTimeout(function() { document.getElementById("demo").innerHTML = i; }, i); i += 500; } }

<body onload="myFunction()">
    <div id="demo"></div>

用于输入元素和模型状态之间的双向绑定。