我有一個非常簡單的組件,可以製作漂亮的小儀表板卡,我可以將它重複用於多個數據片段。當我使用測試數據時,我的情況很好,因爲它立即發送數據並選擇正確的管道。但是,在真正的儀表板上,它需要從API調用數據。數據被返回,但由於這不再是ngOnInit,我的情況不會觸發。有人會知道在從我的服務中獲取數據後,我可能會選擇合適的條件嗎?如何使用異步數據有條件地設置管道角度
組件:
import { Component, OnInit } from '@angular/core';
import {SecondsToMinutesPipe} from "../seconds-to-minutes.pipe";
import { DecimalPipe } from "@angular/common";
@Component({
selector: 'app-dash-stat',
inputs: [
'icon', 'color', 'amount', 'description', 'time'
],
templateUrl: './dash-stat.component.html',
styleUrls: ['./dash-stat.component.css'],
})
export class DashStatComponent implements OnInit {
private loading: boolean = true;
private icon: string;
private color: string = '#fff';
private amount: any = 0;
private description: string;
private time: boolean = false;
constructor(private timePipe: SecondsToMinutesPipe, private numberPipe: DecimalPipe) { }
ngOnInit() {
this.loading = false; //remove this and figure out
if(this.time){
this.amount = this.timePipe.transform(this.amount);
}
else
{
this.amount = this.numberPipe.transform(this.amount, '1.0-0');
}
}
}
我把它從我的儀表盤就像這樣:
<div class="col-sm">
<app-dash-stat icon="fa-tint" color="#eb1c2d" description="Litres Flown" amount="{{dashStats.volume}}"></app-dash-stat>
</div>
<div class="col-sm">
<app-dash-stat icon="fa-clock-o" color="#fd6b00" description="Average Loading Time" amount="{{dashStats.filltime}}" time="true"></app-dash-stat>
</div>
似乎是一個完美的使用情況下,該管:https://angular.io/api/common/AsyncPipe –
哦,這看起來很完美!我會試一試 – enfrost
啊,它不是很有效,因爲異步部分在父組件中,我確信有一些方法可以將可觀察值或承諾傳遞給子組件,但是首次嘗試更改工作。很高興知道新的過濾器壽! – enfrost