Eckige 2 - Interpolation und-Bindung mit asynchronen http-request

Ich bin neu Eckig 2 und ich bin mit einem problem konfrontiert, mit asynchronen http-request und-Bindung mit interpolation.

Hier meine Komponente:

@Component({
  selector: 'info',
  template: `<h1>{{model.Name}}</h1>`
})
export class InfoComponent implements OnInit {

    model: any;

    constructor(
        private _service: BackendService
    ) { }

    ngOnInit() {
         if (this.model == null) {
            this._service.observableModel$.subscribe(m => this.model = m);
            this._service.get();
        }     
    }
}

Wenn das template gerendert wird, bekomme ich eine Fehlermeldung, weil "Modell" ist nicht festgelegt noch.

Habe ich das problem gelöst mit diesem sehr hässlicher hack:

@Component({
    selector: 'info',
    template: `
  <template ngFor #model="$implicit" [ngForOf]="models | async">
  <h1>{{model.Name}}</h1>
  </template>
  `
})
export class NeadInfoComponent implements OnInit {

    models: Observable<any>;

    constructor(
        private _service: BackendService
    ) { }

    ngOnInit() {
         if (this.models == null) {
            this._service.observableModel$.subscribe(m => this.models = Observable.of([m]));
            this._service.get();
        }     
    }
}

Meine Frage ist: wie stellen Sie den template-rendering, bis meine http-Aufruf abgeschlossen ist oder wie interpolieren das "Modell" die Werte direkt in das template ohne die Bindung an eine andere Komponente?

Dank!

Schreibe einen Kommentar