Angular 2 - 突出显示更新的表格单元格

时间:2016-05-05 21:46:13

标签: angular angular2-directives

如何在表格中更改其值的闪存?

<tr *ngFor="#product of products" (click)="onSelect(product)">
    <td>{{product.productName}}</td>
    <td>{{product.price}}</td>
</tr>
组件中的

我有产品输入:@Input() products:Array<ProductModel>; 并且在父组件中有一个测试计时器,每3秒更改一次随机产品的价格:

var timer = Observable.timer(2000, 3000);
    timer.subscribe(t => this._changeRandomProduct());



 private _changeRandomProduct() {
    var productCandidate:ProductModel = this.products[randomOf(0, this.products.length)];
    productCandidate.price = productCandidate.price + 1;
}

如何在价格单元中处理价值变化以在其上添加css类?

1 个答案:

答案 0 :(得分:4)

Plunker example

您可以直接传递索引(或其他方法来识别已更改的项目)并添加/删除与CSS动画一起使用的类

@Component({
  selector: 'my-products',
  styles: [`
.updated {
  animation: blinker 1.7s cubic-bezier(.5, 0, 1, 1) infinite alternate;  
}
@keyframes blinker {  
  from { opacity: 1; color: red;}
  to { opacity: 0; color: black;}
}`],
  template: `
  <tr *ngFor="let product of products; let i = index" (click)="onSelect(product)" [ngClass]="{updated: i === updatedIndex}">
    <td>{{product.productName}}</td>
    <td> - </td>
    <td>{{product.price}}</td>
  </tr>`,
})
export class MyProducts {
  @Input() products:any[];
  @Input() updatedIndex:number;
}