0

I have a module for a unique component with his own routingModule and resolver to pre-fetch data from backend. in the component's ngOnInit() I get the data passed in the route to fill a form. So if I call it with it's route, It works.

The problem is I also want to be able to load it by its selector inside a component of another external module and still pre-fetch data before loading.

the natural solution I got is to do this in the ngOnInit:

ngOnInit() {
  if(comingFromRoot()) {
   this.route.data.subscribe(
     (data: {contactData: any}) => {
      ...
     }     
   );
  } else {
    this.backend.getContactData().subscribe( () => ... );
  }
}

is there any better solution ?

0

1 Answer 1

0

You can use ngIf in that case something like in your parent component html

<your-componenet *ngIf="data" [data]="data"></your-component>

In your componenet get data as input

  @Input() data:any;
  ngOnInit() {
      if(!data) {
      this.route.data.subscribe((data: {contactData: any}) => {
      ...
      });
   }

and In your parent component.ts

data:any;

//following in constructur or ngoninit
this.backend.getContactData().subscribe( (data) => this.data=data );
Sign up to request clarification or add additional context in comments.

Comments

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.