Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 using template reference variable ngForm as input binding

Tags:

angular

I'm building a component that has an input 'valid'. If I bind the value to a member of the parent component, things work well. But if I bind it to a template reference like so

<step [valid]="name.valid">

      <input type="text" name="name"
             #name="ngForm"
             [(ngModel)]="name"
             required>

</step>

I get

Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'

which I partly understand. I get that the ngForm valid check occurs after component initialization, and therefore the value has changed. What I don't get is why this is a problem, and why this can be solved by calling enableProdMode(), and why enableProdMode() is a bad idea.

I also tried ChangeDetectorRef with .detach() and .reattach() to temporary disable change detection, which didn't solve it and sounds like a bad idea too.

Any thoughts?

like image 713
Daco Avatar asked Apr 12 '26 16:04

Daco


1 Answers

That's a known issue. Use instead

<form #f="ngForm">
  <step [valid]="f.controls['name'].valid">
like image 178
Günter Zöchbauer Avatar answered May 10 '26 20:05

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!