Reuse corner 2 shape components

I am currently working on an Angular project and would like to be able to reuse the components of my form for both creating and updating entities.

For example, I have a User entity in a remote API, and I have a form on the external interface that allows me to create these users and send input to the server. It works great!

The problem starts when you try to use the same form for updating. I need to fill in the form input fields with the saved information from the remote server by making a GET request. The problem is that the form component is loaded before the observed REST request responds with the User entity .

How to fill out a form with information from a remote server before the form is fully loaded?

+4
source share
1 answer

The easiest way is to use ngIf

@Component({
  selector: 'xxx',
  template: `
<form *ngIf="model">
  ...
</form>

<!-- optional -->
<my-spinner *ngIf="!model"></my-spinner>
`})
export class MyComponent {
  constructor(private myService:MyService) {
    this.myService.getData.subscribe(data => this.model = data);
  }
}

then *ngIfadds the form to the DOM as soon as it is executed this.model = data.

+2
source

All Articles