Using an array from Observable Object with ngFor and Async Pipe Angular 2

后端 未结 4 556
南笙
南笙 2020-12-08 03:25

I am trying to understand how to use Observables in Angular 2. I have this service:

import {Injectable, EventEmitter, ViewChild} from \'@angular/core\';
i         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 04:04

    Here's an example

    // in the service
    getVehicles(){
        return Observable.interval(2200).map(i=> [{name: 'car 1'},{name: 'car 2'}])
    }
    
    // in the controller
    vehicles: Observable>
    ngOnInit() {
        this.vehicles = this._vehicleService.getVehicles();
    }
    
    // in template
    
    {{vehicle.name}}

提交回复
热议问题