【问题标题】:Enable/Disable Button in angular when I modify form修改表单时以角度启用/禁用按钮
【发布时间】:2023-01-29 21:33:26
【问题描述】:

我正在处理一个错误,我想阻止用户保存他没有在表单中输入任何数据的数据。表单是这样创建的:

private buildAddressPopupForm() {
this.form = this.fb.group({
  roles: [''],
  addressLine1: [this.data.addressLine1],
  addressLine2: [this.data.addressLine2],
  city: [this.data.city],
  state: [this.data.state],
  postalCode: [this.data.postalCode],
  country: [this.data.country],
  startDate: [],
  endDate: [],
  isNew: [this.data.isNew],
  phone: [this.data.phone],
  fax: [this.data.fax],
  mobile: [this.data.mobile],
  email: [this.data.email],
  website: [this.data.website],
  active: [this.data.active === undefined ? true : this.data.active],
});

}

我没有应用和取消按钮,它们来自另一个库,所以我覆盖了它们:

createButtons() {
let applyButton = {
  label: "apply",
  id: "apply",
  disabled: true,
  onClick: () => this.onApplyPopup()
}
let cancelButton = {
  label: "cancel",
  id: "cancel",
  disabled: false,
  onClick: () => this.onClosePopup()
}
this.buttons.push(applyButton);
this.buttons.push(cancelButton);

}

onApplyPopup() {
if (this.form.valid) {
  this.applyPopup.emit(this.form.value);
}

}

onClosePopup() {
    this.closePopup.next();
  }

在我点击 Apply 之后,一个事件发射器被发送并且数据被推送到一个数组中。

这 2 个方法:createButtons() 和 buildAddressPopupForm() 在 ngOnInit 中调用,Apply 按钮默认禁用。我希望在更改表单时启用我的按钮,但我不知道如何操作,因为按钮是一次性创建的,而 ngOnInit 仅在创建组件时被调用。我尝试使用事件发射器,但没有得到结果。我们将不胜感激任何帮助,谢谢

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    试试这个方法!

    ngOnInit() {
      this.myForm = this.formBuilder.group({
        name: '',
        email: '',
        message: ''
      });
    
      this.onChanges();
    }
    
        onChanges(): void {
          this.myForm.valueChanges.subscribe(val => {
            //Do your stuff here
        }
    

    【讨论】:

    • 你好,我不明白你的回答,我是 Angular 的新手。我知道什么是脏的和被触摸的,但我不明白如果她被禁用,我怎么能检查表格是否被触摸了..
    • 请参阅此处的文档:angular.io/api/forms/…
    • 如果她被禁用,我如何触摸表格?我不明白你的建议和你的方法
    • 表单有一个名为 touched 的属性。它返回布尔值。仅在触摸表单时启用按钮。
    • 我在 ngOnInit 中创建表单和按钮,默认情况下禁用 applyButton。当我从表单更改某些值时,我想要一种启用该按钮的方法,但我不能使用 valuesChanges 或 statusChanges 执行此操作,因为我必须在 ngOnInit 中订阅它们并且它不会更改按钮状态。如果我在 ngOncHanges 中订阅相同,它不起作用..我无法在按钮上调用表单的 touched 属性,因为我在 ngOnInit 中创建了该按钮并且组件只创建了一次
    【解决方案2】:

    您可以使用 FormGroupstatusChanges Observable 来实现这一点。

    首先,您需要跟踪您的按钮:

    createButtons() {
      // create this property in your class
      this.applyButton = {
        label: "apply",
        id: "apply",
        disabled: true,
        onClick: () => this.onApplyPopup()
      }
    ...
    }
    

    然后在你的ngOnInit 中订阅可观察对象并更新按钮的禁用状态:

    ngOnInit() {
      this.form.statusChanges.subscribe(status => {
        if (status === 'VALID' && this.form.touched) {
          this.applyButton.disabled = false;
        }
      });
    }
    

    【讨论】:

    • 嘿,谢谢你的回复。我试过这种方法,还是不行。创建按钮的 createButtons() 函数也在 ngOnInit() 中调用,最初按钮被禁用。之后我按照你说的在 ngOnInit 中订阅,按钮将被启用。所以最后即使我没有更改表单中的任何内容,按钮也会被启用
    • 你可以使用 this.form.valuechanges.subscribe((res) =>{ console.log(res) });你可以把它放在 ngOnInit() 的末尾,或者你可以使用 ngOnChanges()
    • 如果我放在 ngOnInit 的末尾,按钮将具有来自 valueChanges.subscribe 的值,因为 ngOnInit 只被触发一次。而在 ngOnChanges 中,我需要使用 @Input() 字段
    • @EugenGîrlescu 如果是这种情况,您可以为触摸添加条件。我编辑了我的答案以反映这一点。
    猜你喜欢
    • 2020-06-05
    • 2021-08-13
    • 1970-01-01
    • 2017-12-14
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 2021-06-03
    相关资源
    最近更新 更多