【问题标题】:How to keep the field enabled if it has a value after edit in angular2如果在angular2中编辑后具有值,如何保持字段启用
【发布时间】:2019-01-02 08:23:48
【问题描述】:

如果我在第一个字段下拉列表中选择第三个下拉值,则启用第二个字段。 所以,现在如果我通过选择下拉列表的第三个值来给出值,并给第二个字段一个值,它就会被采用。 现在我去编辑函数并更改一些其他值,因为第二个字段被禁用,我得到 ass null 或 undefined。 所以现在如果第一个字段在编辑模式下没有更改,我希望第二个字段有它的值

HTML:

TS:

private FormInit() {
    //Add
    if (!this.Details) {
      this.detailsForm = this.FB.group({
        Type: ['', Validators.required],
        ID: [{ value: 0, disabled: true }],
      });

    } else {
      // edit
      this.detailsForm = this.FB.group({
        Type: [this.Details.data.Type, Validators.required],
        SourceID: [{ value: this.Details.data.ID, disabled: true }],

      });
      if (this.Details.mode == 'readOnly') {
        this.detailsForm.disable();
      }
    }
  }

添加和编辑功能:

public save() {
    let addParams = this.detailsForm.value;
   addParams.Type = addParams.Type ? addParams.Type: 0;
    this.Service.add(addParams).subscribe(res => {
      console.log(res);
      this.successMessagePopUp(res);
    })
  }

  public update() {
    let updateParams = this.detailsForm.value;

    this.Service.update(updateParams).subscribe(res => {
      this.successMessagePopUp(res)
    })
  }

希望通过一种方式解决这个问题,如果选择了第三个下拉值,那么在编辑模式下我们可以保持第二个字段启用并在选择其他值时禁用吗?

【问题讨论】:

  • 什么时候你得到空值?提交时?
  • @Sravan 我更新了我的问题,请检查
  • @Sravan 这个逻辑会起作用吗?
  • @Sravan 请检查 sourceTypeValidation(),如果值为 3,则启用源 ID 字段,否则将禁用源 ID,因此该字段不传递,因此其值为空跨度>

标签: angular typescript


【解决方案1】:

通常disabled 值将在angular 中显示未定义。

PS:我想根据你的要求:

private FormInit() {
    if (!this.Details) {
      this.detailsForm = this.FB.group({
        Type: ['', Validators.required],
        ID: [{ value: 0, disabled: true }],
      });

    } else {
      // edit
      this.detailsForm = this.FB.group({
        Type: [this.Details.data.Type, Validators.required],
        ID: [{ value: this.Details.data.ID }],

      });

     if (this.Details.data.Type == 3)
     {
         this.detailsForm.get("ID").enable();
     }
     else
     {
         this.detailsForm.get("ID").disable();
     }

      if (this.Details.mode == 'readOnly') {
        this.detailsForm.disable();
      }
    }
  }

【讨论】:

  • 感谢您的回复我检查
  • 我会检查的
  • 您没有在哪里获得禁用值?
  • 在 soueceId 字段中
  • 我编辑了一些其他字段,但 sourceID 中的值也在变化
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
相关资源
最近更新 更多