无法使用数组符号功能访问表单控件

时间:2018-06-21 10:05:26

标签: angular

import { Component, OnInit } from '@angular/core';
import { GetTransactionService } from '../../../service/getTransaction/get-transaction.service';
import { Router, Routes, RouterModule } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-transactions',
  templateUrl: './transactions.component.html',
  styleUrls: ['./transactions.component.css']
})
export class TransactionsComponent implements OnInit {
    form: FormGroup;

    constructor(private _data: GetTransactionService, private formBuilder: FormBuilder) { }

    ngOnInit() {
        this.form = this.formBuilder.group({
            ordSearch: ['', Validators.required],
            fromDate: ['', Validators.required],
            toDate: ['', Validators.required],
            systemId: ['', Validators.required],
            entityId: ['', Validators.required],
            pmntMethod: ['', Validators.required],
            merchantId: ['', Validators.required],
            ordStatus: ['', Validators.required],
            refNumber: ['', Validators.required]

        });
   }

  public responseJSON;

  getTransactionDtls () {
      let transactionResponse = this._data.getTransactionDtls().subscribe(
          success => {
              console.log(success);
              this.responseJSON = success;
             // this.populateOrder(success);
          }, error => {
              console.log(error);
          }
      );;
  }


  datePopulate = (event) => {
      let target = event.target || event.srcElement || event.currenTarget;
      let label = target.innerText;
      let currentDate = new Date();
      let date = currentDate.getDate();
      let month = currentDate.getMonth();
      let year = currentDate.getFullYear();
      let currentDateFormat = date + "-" + month + "-" + year;

      switch (label) {
          case "Today": {
              this.form.controls[fromDate].setValue(currentDateFormat);
          }
          case "Yesterday": {

          }
          case "This Week": {

          }
          case "Last Week": {

          }
          case "This Month": {

          }
          case "Last Month": {

          }
      }
  }

}

this.form.controls[fromDate].setValue(currentDateFormat),我在fromDate中遇到“找不到名称”错误,此功能与datePopulate = function (event) { and changed the value set as this.form.controls.fromDate.setValue(currentDateFormat);之类的普通功能一起工作

请帮助我使用数组符号功能设置值

1 个答案:

答案 0 :(得分:0)

将其包装在''

周围
 this.form.controls['fromDate'].setValue(currentDateFormat);