代码之家  ›  专栏  ›  技术社区  ›  nightingale2k1

角度6反应形式-如何设置条件值?

  •  1
  • nightingale2k1  · 技术社区  · 6 年前

    this.editForm = this.fb.group(
          {
            firstName: [this.user.firstName, Validators.required],
            lastName: [this.user.lastName, Validators.required],        
            workingDate: [
                moment(this.user.workingDate).format("DD/MM/YYYY"),
              Validators.required
            ], ....
           }
    

    我想把一个条件this.user.workingDate==“”然后将其留空(如果我们使用moment(),它将显示当前日期)

    怎么做?

    3 回复  |  直到 6 年前
        1
  •  1
  •   Antoine V    6 年前

    你能试试这个吗:

    workingDate: [
              this.user.workingDate ? moment(this.user.workingDate).format("DD/MM/YYYY") : null,
              Validators.required
            ], ....
    
        2
  •  1
  •   Sivakumar Tadisetti zeah    6 年前

    使用terinary运算符,可以设置如下条件

    workingDate: [
       (this.user.workingDate ? moment(this.user.workingDate).format("DD/MM/YYYY") : ''), Validators.required
    ]
    

    Stackblitz example

        3
  •  0
  •   Bhumika Barad    6 年前
    this.editForm = this.fb.group(
      { workingDate: [this.user.workingDate ? moment(this.user.workingDate).format("DD/MM/YYYY") : null, Validators.required],
        mobileNo: [this.user.mobileNo ? this.user.mobileNo : null, [Validators.maxLength(11), Validators.minLength(10)]]
       }
    

    从'moment'导入*作为moment