0

My parent component html contains this line to call a child component with a default value of maxPrice:

<app-filter-events [maxPrice]='_maxPrice'></app-filter-events>

The parent component is getting the maxPrice by calling an API before instantiating the child component, here is the code :

constructor(private _dataService: DataService) {
 this._dataService.getEventsByCriteria(this._filterCriteria).subscribe(res => this._maxPrice = res);
}

The problem is that maxPrice is undefined in the child component. I guess that the problem come from the async call to the API but I have no idea to resolve it.

Thanks

EDIT : My problem is not to hide the child component if the maxPrice is undefined but to have the maxPrice setted before instantiating the child component.

2
  • wat is the type of _maxPrice? Commented Mar 27, 2018 at 13:48
  • 2
    Try to implement NgOnInit interface. Commented Mar 27, 2018 at 13:48

5 Answers 5

2

It's quite simple make a variable set it to false before you call the call and set it to true after the async is done. You don't want the component to be loaded in the dom, before you're done.

<div *ngIf="maxPriceBoolean">
  <app-filter-events [maxPrice]='_maxPrice'></app-filter-events>
</div>
Sign up to request clarification or add additional context in comments.

Comments

2

You can not instantiate app-filter-event component at the time _maxPrice is still undefined:

<app-filter-events *ngIf='_maxPrice' [maxPrice]='_maxPrice'></app-filter-events>

2 Comments

I need to have the maxPrice, I think that the child component is instantiated before getting the maxPrice. How to wait for the maxPrice before instantiating the child component ?
' wait for the maxPrice before instantiating the child component' => that exactly what the *ngIf in this code does
1

Simply use *ngIf :

<app-filter-events *ngIf='_maxPrice' [maxPrice]='_maxPrice'></app-filter-events>

OR

<ng-container *ngIf="_maxPrice">
   <app-filter-events [maxPrice]='_maxPrice'></app-filter-events>
</ng-container>

3 Comments

I need to have the maxPrice, I think that the child component is instantiated before getting the maxPrice. How to wait for the maxPrice before instantiating the child component ?
@zak , this will do the same thing this will check if maxPrice is instantiated , if yes it will load the component else not.
@Zak , Glad to know that it helped you , will you please also accept the answer ?
0

constructor executes before @Input(), so the binding Input value is undefined in your constructor block, make your component implements OnInit and access @Input() variable in ngOnInit() method, it would be ready to use.

Comments

0

use async pipe :

<app-filter-events [maxPrice]='_maxPrice$ | async'></app-filter-events>

component code

    _maxPrice$: Observable<number>;
   constructor(private _dataService: DataService) {
     this._maxPrice$ = this._dataService.getEventsByCriteria(this._filterCriteria)
    }

2 Comments

@zac Please try this. No need for adding any *ngIf
Your answer seems to be elegant but I need the maxPrice to be number, not an observable. Thank you

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.