1

I have a form created with formbuilder in my SettingsComponent:

constructor(){
    this.userForm = this.formBuilder.group({
        'firstname': ['', Validators.required],
        'lastname': ['', Validators.required],
        'email': ['', Validators.compose([Validators.required, ValidationService.emailValidator])]
    });
}

what I want to do now is add a class to my input when the value is not valid. But when I do this:

<label class="input" [ngClass]="{'state-error': lastname.valid}">
    <input ngControl="lastname" [(ngModel)]="user.lastname" id="lastname" #lastname="ngForm"/>
</label>
<small [hidden]="lastname.valid" class="form-required-field-info">this is a required field</small>

this produces the following error:

Expression '{'state-error': lastname.valid} in SettingsComponent@60:57' has changed after it was checked. Previous value: 'null'. Current value: 'false'

Link to demo: https://plnkr.co/edit/WvycldydiuMsi8HwgIYc

What am I missing? Any hints? Thanks in advance!

3 Answers 3

1

It's not caused by lastname.valid but by changing the model in ngAfterViewInit() Angular doesn't like the model being changed in lifecycle callbacks.

If you change

  ngAfterViewInit() {
    this.user = {'lastname': 'test'};
  }

to

  ngAfterViewInit() {
    setTimeout(() => this.user = {'lastname': 'test'});
  }

the error is gone. No need to change the view template.

Plunker example

Sign up to request clarification or add additional context in comments.

13 Comments

thank you. I tried that but that returns the same error
Can you create a Plunker?
this is the first time I tried this, hope it works: plnkr.co/edit/WvycldydiuMsi8HwgIYc
Seems to work plnkr.co/edit/W8CEGiw8V5H4Nn72NQUY?p=preview the way I explained in my answer.
You are right. I tried a few times and didn't get any errors but now when I reopen I get them as well. I'll have another look.
|
0

seems like I found the solution (I think). What I need to do is manually detect changes. So I import ChangeDetectorRef from angular/core, inject it in the constructor and then, after importing the form data, I do this:

this.ref.detectChanges();

Comments

0

Please verify that you are not running in dev mode. In dev mode, such errors are encountered when change detection kicks in immediately after ngViewInit. (Even though this is the expected behavior in Dev Mode, to identify possible errors in out code, but there is a bug open #6005)

So for now, enable prod mode, where in change detection automatically kicks in every time a change happens.

2 Comments

That's a bad advice. devMode exists for a purpose and it's to find bugs that make change detection inefficient.
I see. The link is a perfect fit to the question. Disabling devMode is still the wrong approach.

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.