简体   繁体   English

Angular2指令未响应异步数据

[英]Angular2 directive is not responding to async data

I have a component that receives and observable as an input. 我有一个可以接收并作为输入的组件。

@Input() data: SubDayFoodVM;

Which was passed from it's parent using the async pipe 使用异步管道从其父级传递来的

<sub-day-food *ngIf="subDay" [data]="data | async"></sub-day-food>

One property on the SubDayFoodVM interface is foodName which gets passed to a custom directive on the template. SubDayFoodVM界面上的一个属性是foodName ,它被传递给模板上的自定义指令。

<div ticker [text]="data.foodName" [size]="30"></div>

This directive creates an element with the [text] passed into it. 该伪指令创建一个带有[text]的元素。 If the text is longer than it's container then then a duplicate element is created and the text margin slides to the left like a news ticker. 如果文本比其容器长,那么将创建一个重复元素,并且文本边距像新闻自动收录器一样向左滑动。 Here is a primitive version that I created a while ago: PLUNKR . 这是我前一段时间创建的原始版本: PLUNKR

In this implementation the text is set onInit 在此实现中,将文本设置为onInit

ngOnInit(): void {
    // ... 
    this.firstNode = this.createTickerNode( this.text );
}

This has resulted in the text not updating when the observable data updates. 这导致可观察数据更新时文本未更新。 When the ticker action is triggered, the newly created element contains the new value, but the original element remains. 触发股票行情操作时,新创建的元素包含新值,但原始元素保留。

How can I alter my implementation to update the text or re-render the directive when the asynchronous data changes? 异步数据更改时,如何更改实现以更新文本或重新呈现指令?

您需要实现OnChange生命周期挂钩。

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

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