Get 2024 Templates Mega Bundle!$1000 worth of 19 Bootstrap HTML, Vue & React Templates + 3 Vector Sets for just $99
Get for 99$

Angular v8.1.9: Reactive Form not responding when value changes


I have a problem building a form using reactive form. The template is not responding as soon as the value changes only after I focus on an input the form is updated.

For example:

// app.component.html
< form [formGroup]="profileForm" (ngSubmit)="onSubmit()">
<label for="first-name">First Name: </label>
< input id="first-name" type="text" formControlName="firstName" required/>

<label for="last-name">Last Name: </label/>
< input id="last-name" type="text" formControlName="lastName"/>

<p>Complete the form to enable button.</p>
<em> Button is invalid</em>
< button type="submit" [disabled]="!profileForm.valid">Submit</button>
<hr>
<em>Not showing value right away</em>
<p>Form Value: {{ profileForm.value | json }}</p>
<em>Not showing value right away</em>
<p>Form Status: {{ profileForm.status }}</p>
</form>



// app.component.ts
this.profileForm = this.fb.group({
firstName: ["", Validators.required],
lastName: [""],
});
setTimeout(() => {
this.profileForm.patchValue({
firstName: "Robert",
lastName: "Landis",
});
console.log(this.profileForm.value);
}, 1000);


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (3)


Hi Matthew,

It seems like the issue is related to the Change Detection Strategy being set to OnPush in your app.component.ts. This strategy can sometimes cause the view to not respond immediately when the value changes.

To resolve this, you can try removing the OnPush strategy or handle the change detection manually. In your app.component.ts, you can do either of the following:

1. Remove the Change Detection Strategy:

// Remove this line if you have set the Change Detection Strategy to OnPush
// import { ChangeDetectionStrategy } from "@angular/core";

@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"],
// Remove the Change Detection Strategy if set to OnPush
// changeDetection: ChangeDetectionStrategy.OnPush,
})


2. Or manually trigger change detection:

import { ChangeDetectorRef } from "@angular/core";

// ...

constructor(private fb: FormBuilder, private cdr: ChangeDetectorRef) {
this.profileForm = this.fb.group({
firstName: ["", Validators.required],
lastName: [""],
});

setTimeout(() => {
this.profileForm.patchValue({
firstName: "Robert",
lastName: "Landis",
});
console.log(this.profileForm.value);
this.cdr.detectChanges(); // Manually trigger change detection
}, 1000);
}


By manually triggering the change detection with this.cdr.detectChanges();, the view should update immediately when the value changes. This should resolve the issue with the form not responding as expected.

Please give this a try and let me know if it resolves the problem. If you have any further questions or encounter any other issues, feel free to ask for more assistance.

Thanks



Thanks for you help! I already solved it.



Hi happy,

Great! If you need any further help please let us know.

All the best with your projects!

Regards.


Your Support Matters!

We will highly appreciate your Metronic Review on Themeforest.
Please go to Themeforest Downloads page, select Metronic & leave your feedback.
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(