Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

361
Visualizações
Can't Reset Input Value Based on Select Field When Form Split into Child Components

I'm showing and hiding a FormControl based on another FormControl in the same FormGroup, which works using the method below, but when I try to reset the input being hidden so on submit it's value is not sent I get the error below.

Component Method

public hasOtherFundingSource(index: number) {
  const formGroup = this.overviewFunds.at(index);
  const source = formGroup.get('source').value;

  if (source !== 'other') {
    formGroup.get('other_source_desc').reset(); // Throws error
    return false;
  } else {
    return true;
  }
}

Error

ExampleComponent.html:12 ERROR Error: 
ExpressionChangedAfterItHasBeenCheckedError: 
Expression has changed after it was checked. 
Previous value: 'true'. Current value: 'false'.

With some help form @yurzui I created a plunker that shows the error. Just change other to another in the select field and watch the console log out the error. The error seems to be related to splitting the FormGroup into sub-components to reduce the size and logic in each class.

If you look at the first plunker that was created the error doesn't occur when the form is encapsulated in the app-component.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You should avoid any side effect within functions that will be executed on every view checking

<div *ngIf="hasOtherFundingSource(i)">

hasOtherFundingSource function will be executed twice in dev mode on every application tick.

There is NgControlStatus directive that checks validation status.

On the first check your form is valid

enter image description here

After that you're calling formGroup.get('other_source_desc').reset(); and status becames invalid

enter image description here

Then angular is running view.checkNoChanges() and you get ExpressionChangedAfterItHasBeenCheckedError

So according to https://angular.io/docs/ts/latest/guide/template-syntax.html#!#no-visible-side-effects

No visible side effects

A template expression should not change any application state other than the value of the target property.

This rule is essential to Angular's "unidirectional data flow" policy. You should never worry that reading a component value might change some other displayed value. The view should be stable throughout a single rendering pass.

To solve your problem i did the following:

1) Added ngModelChange event to select control

<select formControlName="source" (ngModelChange)="sourceChanged($event, i)">

2) Move side effect from hasOtherFundingSource to sourceChanged function

sourceChanged(value: string, index: number) {
  if (value !== 'other') {
      const formGroup = this.overviewFunds.at(index);
      formGroup.get('other_source_desc').reset();
  }
}

Modified Plunker

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda