【问题标题】:How to bind three different input using ngModel in angular?如何使用 ngModel 以角度绑定三个不同的输入?
【发布时间】:2020-01-29 19:31:58
【问题描述】:

我有 3 个不同的 ngModel,它们在模态窗口中按顺序显示,所以当我完成更改时,CURRENT_TEXTALLERGY_DETAILS 总是得到相同的值,有没有更好的方法来使用 ngModel 来完成这项任务或者我错过了什么?

app.component.html

<div class="form-group" *ngIf="showSubQuestions">
    <div  *ngFor="let option of singleQuestion.answerOption">
    <div *ngFor="let sub of option.subQuestion">
        <label for="subQuestionsInput">{{sub.questionText}}</label>
        <input type="subQuestionsInput" class="form-control"  name="allerdydetail" placeholder="answerText" [(ngModel)]="ALLERGY_DETAILS.answerText">
    </div>
    </div>
</div>
<div class="form-group" *ngIf="showSubQuestionsCommon">
    <div  *ngFor="let option of singleQuestion.answerOption">
        <div *ngFor="let sub of option.subQuestion">
        <label for="subQuestionsCommon">{{sub.questionText}}</label>
        <input type="subQuestionsCommon" class="form-control"  name="currentText" placeholder="Text" [(ngModel)]="CURRENT_TEXT.answerText">
        </div>
    </div>
</div>
<div class="form-group" *ngIf="showSubQuestionsQues"> 
<div  *ngFor="let option of singleQuestion.answerOption">
    <div *ngFor="let sub of option.subQuestion">
        <div *ngFor="let ques of sub.question; let i = index; trackBy:trackByIndex;">
            <label for="subQuestionsInput">{{ques.questionText}}</label>
            <!-- <input class="form-control" [(ngModel)]="ques.question[i]" [ngModelOptions]="{standalone: true}"> -->
            <input type="subQuestionsInput" class="form-control" *ngIf= "ques.answerType === 'TEXT_NUMBER'" name="TEXT_NUMBER" placeholder="Enter Dose amount Left"  [(ngModel)]="TEXT_NUMBER.answerText">
            <input type="subQuestionsInput" class="form-control" *ngIf= "ques.answerType === 'TEXT_DATE'" name="TEXT_DATE" placeholder="Enter Next Dose Date" [(ngModel)]="TEXT_DATE.answerText"> 
        </div>
    </div>
    </div>
</div>

app.component.ts

import { Component, ViewChild, ElementRef} from '@angular/core';
import { ApiService } from './api.service';
import { EventService} from './format-questions/format-questions.service'

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  @ViewChild('closeBtn', {static: false}) closeBtn: ElementRef;
  data: any;
  questions: any[] = [];
  singleQuestion: any[] = [];
  showSaveButton: boolean = false;
  showNextButton: boolean = true;
  showSubQuestions: boolean = false;
  currentSelectedItem: any;
  questionsOptionSelected: boolean = false;
  showSubQuestionsQues: boolean = false;
  questionsArray: any =[];
  subQuestionsAnswertext: any = [];
  TEXT_DATE: any;
  TEXT_NUMBER: any;
  CURRENT_TEXT: any;
  ALLERGY_DETAILS: any;
  showSubQuestionsCommon: boolean = false;

  constructor(private dataService:ApiService, private eventService:EventService) {
    this.questions = this.dataService.getData();
      this.TEXT_DATE = {
        "answerOptionId": 0,
        "answerText": "",
        "answerOptionId2": 0
    };
      this.TEXT_NUMBER = {
        "answerOptionId": 0,
        "answerText": "",
        "answerOptionId2": 0
    };
      this.CURRENT_TEXT =  {
        "answerOptionId": 0,
        "answerText": "",
        "answerOptionId2": 0
    };
      this.ALLERGY_DETAILS = {
        "answerOptionId": 0,
        "answerText": "",
        "answerOptionId2": 0
    };
  }
}

【问题讨论】:

  • 你应该看看Reactive Forms
  • @DanielHabenicht 上面代码的任何示例,对于 Angular 来说都是新的

标签: javascript angular typescript twitter-bootstrap


【解决方案1】:

只需使用上面评论中建议的反应形式, 在你的组件中创建一个新的 ALLERGY_DETAILS formControl:

component.ts

ALLERGY_DETAILS: FormControl = new FormControl('',Validators.required)
// remove the second parameter in case the value is not required
// to get the input value use 
this.ALLERGY_DETAILS.value;

然后在 HTML 模板中将输入元素与控制器链接:

component.html

<input type="subQuestionsInput" class="form-control"
  name="allerdydetail" placeholder="answerText" 
  [formControl]="ALLERGY_DETAILS">

最后在你的 app.module.ts 中导入 ReactiveFormsModule

【讨论】:

  • 我试过了,你解释说它适用于 ALLERGY_DETAILSCURRENT_TEXT 总是作为空字符串出现,TEXT_NUMBERTEXT_DATE 都有相同的值知道吗?
  • 您是否从CURRENT_TEXT 中删除了 [(ngModel)] 吗?同样对于NUMBER, DATE,您可能将两个不同的输入链接到同一个控制器
  • 是的 ngModel 已从输入中删除,所有这些都在一个组件中,同一个控制器的两个不同输入是什么意思,这些都在一个“表单”中
  • 使用 FormGroup 提出了另一个问题,如果您可以帮助仍然试图找到解决方案 stackoverflow.com/questions/59990604/…
  • 如果您想将您的表单拆分为小的 FormGroups(因为您有 *ngFor ,请使用 FormGroups 的 FormArray 并在每个组内创建所需的 formControls ,然后将输入标签链接到控制器,您可以这样做这个:formarray[i].controls['controlerName']
猜你喜欢
  • 2021-07-25
  • 2018-06-07
  • 1970-01-01
  • 1970-01-01
  • 2019-04-20
  • 2020-06-24
  • 2017-08-16
  • 1970-01-01
  • 2019-06-26
相关资源
最近更新 更多