简体   繁体   English

如何使用异步管道在Angular2组件的视图中使用单个Observable属性?

[英]How to consume single Observable property in view from Angular2 component using async pipe?

If I have a component with a property of: 如果我的组件具有以下属性:

import { Component, OnInit } from 'angular2/core';
import { CarService } from 'someservice';

@Component({
    selector: 'car-detail',
    templateUrl: './app/cars/car.component.html'
})

export class CarComponent implements OnInit {

    model: Observable<Car>;

    constructor(
        private _carService: CarService
    ) { }

    ngOnInit() {
        // begin benefit setup
        this.model = this._carService.get(5);
    }
}

for instance, and I want to in my view use an async pipe to subscribe to that and use the properties as text, how can you actually subscribe to it? 例如,在我看来,我想使用一个异步管道来订阅它并使用属性作为文本,那么如何实际订阅它呢? For instance: 例如:

<template #mycar = "model | async" >
    <p>{{ myCar?.make }}</p> 
</template>

I am new to Angular2 and not sure if I am doing something wrong or not. 我是Angular2的新手,不确定自己是否做错了事。

I believe you were close to a good solution. 我相信您已经接近一个好的解决方案。

 <template *ngIf = "model | async as mycar" > <p>{{ mycar.make }}</p> </template> 

Try: 尝试:

<template #mycar = "model | async" >
    <p>{{ (myCar | async).make }}</p> 
</template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM