3

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?

1 Answer 1

3

That's a known issue. Use instead

<form #f="ngForm">
  <step [valid]="f.controls['name'].valid">
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. If I try that I get Error: Uncaught (in promise): Cannot assign to a reference or variable!
Some more details what can not be assigned to what?
What Angular2 version are you using? How does your <form> tag look like?
I use 2.0.0-rc.1 and the form looks like <form #f="ngForm">. I'm sorry, I think that error was unrelated. Do I also need to keep the #name="ngForm" on the input element? If I do that I get Uncaught (in promise): Template parse errors: There is no directive with "exportAs" set to "ngForm" (" If I don't, I get TypeError: Cannot read property 'valid' of undefined
No, you don't need #name="ngForm" but you need some way to make the input an Angular form control. Try adding ngControl="name" then #name="ngForm" shouldn't throw anymore (in case you need it for something else).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.