Angular响应式表单 – 控件disable()

huangapple go评论53阅读模式
英文:

Angular Reactive Form - Control disable()

问题

我可以帮你翻译你提供的内容:

我无法找到解决我的特定问题的方法,我有一个响应式表单,在其中我想根据单选按钮的更改禁用选择字段,但`disable()`函数不起作用。

这是我的表单:

```typescript
  ngOnInit() {
    this.createForm();
  } 

  createForm(): void {
    this.bsiNotesForm = this.formBuilder.group({
      searchType: new FormControl(this.searchCriteriaEnum.AllNotes),
      newNotes: new FormControl(false),
      bsiNumberToSearch: new FormControl(''),
      modelsToSearch: new FormControl({ value: '', disabled: true }),
    });
  }

这是切换函数,我试图在searchType等于searchCriteriaEnum.AllNotes时禁用选择字段:

  toggleSearchType() {
    const searchType = this.bsiNotesForm.get('searchType')?.value;
    if (searchType === this.searchCriteriaEnum.ByModel) {
      console.log(searchType)
      this.bsiNotesForm.get('modelsToSearch')?.enable();
    } else {
      this.bsiNotesForm.get('modelsToSearch')?.disable();
      console.log(searchType)
      this.bsiNotesForm.get('modelsToSearch')?.reset();
    }
  }

这是选择和单选按钮的模板:

 <custom-select
  [name]="'modelsToSearch'"
  formControlName="modelsToSearch"
  [options]="modelsData"
 >
 </custom-select>

 <custom-radio
  [options]="[
  { value: searchCriteriaEnum.AllNotes, label: 'All Notes' },
  { value: searchCriteriaEnum.ByModel, label: 'By Model' }]"
  formControlName="searchType"
  (change)="toggleSearchType()"
  >
 </custom-radio>

如果需要更多的代码和解释,请随时提问!

英文:

I can't find a solution to my specific Problem i have my Reactive Form where i want to disable select field based on radio element change and disable() function is not working.

Here is my form:

  ngOnInit() {
    this.createForm();
  } 

  createForm(): void {
    this.bsiNotesForm = this.formBuilder.group({
      searchType: new FormControl(this.searchCriteriaEnum.AllNotes),
      newNotes: new FormControl(false),
      bsiNumberToSearch: new FormControl(&#39;&#39;),
      modelsToSearch: new FormControl({ value: &#39;&#39;, disabled: true }),
    });
  }

Here is the toggle Function where im trying to disable the Select Field when the searchType is equals to searchCriteriaEnum.AllNotes

  toggleSearchType() {
    const searchType = this.bsiNotesForm.get(&#39;searchType&#39;)?.value;
    if (searchType === this.searchCriteriaEnum.ByModel) {
      console.log(searchType)
      this.bsiNotesForm.get(&#39;modelsToSearch&#39;)?.enable();
    } else {
      this.bsiNotesForm.get(&#39;modelsToSearch&#39;)?.disable();
      console.log(searchType)
      this.bsiNotesForm.get(&#39;modelsToSearch&#39;)?.reset();
    }
  }

here is the select and radio template


 &lt;custom-select
  [name]=&quot;&#39;modelsToSearch&#39;&quot;
  formControlName=&quot;modelsToSearch&quot;
  [options]=&quot;modelsData&quot;
 &gt;
 &lt;/custom-select&gt;

 &lt;custom-radio
  [options]=&quot;[
  { value: searchCriteriaEnum.AllNotes, label: &#39;All Notes&#39; },
  { value: searchCriteriaEnum.ByModel, label: &#39;By Model&#39; }]&quot;
  formControlName=&quot;searchType&quot;
  (change)=&quot;toggleSearchType()&quot;
  &gt;
 &lt;/custom-radio&gt;

if i need to provide more code and explanation feel free to ask !

答案1

得分: 0

我理解到您需要根据自定义单选按钮的更改禁用和启用表单的modelsToSearch字段。这似乎没问题,但由于您同时使用了custom-radio和custom-select字段,我担心它们的实现可能会出现潜在问题。另一点需要考虑的是订阅searchType值的更改。

英文:

What I understood is that you need to disable and enable the modelsToSearch field of your form depending on the change in the custom-radio selection. This seems okay, but since you have both custom-radio and custom-select fields, I am worried about a potential issue in their implementation. Another thing to consider is subscribing to the searchType valueChanges.

答案2

得分: 0

抱歉,我没有回答评论,因为我在处理我的应用程序的其他部分。

我在这个主题中找到了解决我的问题的方法:
https://stackoverflow.com/questions/40163367/angular-2-custom-form-control-disable

问题是我没有在我的自定义选择组件中使用setDisbledState()函数。在使用控制值访问器时,这很重要。

TS文件:

disabled = false;

setDisabledState(isDisabled: boolean) {
    this.disabled = isDisabled;
}

HTML文件:

<select [disabled]="disabled">
    <option> ... </option>
</select>
英文:

Sorry for not answering the comments i was working on other parts of my application.
I found solution to my Problem with help of this topic
https://stackoverflow.com/questions/40163367/angular-2-custom-form-control-disable

The problem was that i didnt used the setDisbledState() function in my custom select component. It is important when we are working with Control Value Accessor.

TS file:

    disabled = false;

    setDisabledState(isDisabled: boolean) {
        this.disabled = isDisabled;
    }

HTML file:

     &lt;select [disabled]=&quot;disabled&quot; &gt;
        &lt;option&gt; ... &lt;/option&gt;
     &lt;/select&gt;
     

huangapple
  • 本文由 发表于 2023年5月7日 21:53:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定