1

ApplicationComponent:

@RouteConfig([
  {path:'/', name: 'Dashboard', component: DashboardComponent, useAsDefault: true},
  {path:'/model/:id', name: 'ModelDetail', component: ModelDetailComponent},
])

The visitor directly visit: http://localhost:8080/model/4

Before we can render "ModelDetailComponent", we need to load all models inside the class ngOnInit().

ngOnInit() {
    let id = parseInt(this.routeParams.get('id'));
    this.modelService.subject$.subscribe(models => {
      if ( models.length > 0 ) {
        this.model = models[0];
      }
    })
    this.modelService.getModel(id);
  }

template:

<h1>{{model.name}}</h1>
<model-list [model]="model"></model-list> //nested compnent that also use the model.

property

public model:IModel;
Jamal James
  • 91
  • 2
  • 9

2 Answers2

1

I think what you are looking for is the CanActivate hook. It will allow you to wait with component rendering until data is resolved (similar to Angular 1.x resolve feature).

See this for more details.

Community
  • 1
  • 1
Yaniv Efraim
  • 6,633
  • 7
  • 53
  • 96
1

Just use

<h1>{{model?.name}}</h1>

(added ?) then it doesn't matter that the data arrives later.

If you have lots of such bindings it might be easier to wrap the whole template with *ngIf

@Component({
  selector: 'my-component',
  template' `
    <div *ngIf="model">
      <h1>{{model.name}}</h1>`})
    <div>
class MyComponent {  }
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567