每次创建新的对象实例?

时间:2018-01-10 10:35:45

标签: html angular typescript object

我的 HTML

中有这个
<div class="ui-g" *ngFor="let orderTracking of orderTrackings, let i=index" (click)="selectItemForRequest(i)" [ngClass]="{'active' : selectedOrderTracking===i }">
                <technical-destinations *ngIf="orderTrackings.length" class="technical_destinations {{i}}" [orderTracking]="orderTracking" [groupDestinations]="groupDestinations"></technical-destinations>
            </div>

在技术目的地,我有dropdown,但当我改变其中一个时,我会改变。问题是在orderTracking中所有对象都是相同的。它只是副本。 我有什么建议可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我尝试通过使用@Input s迭代组件来重现您的问题。

所以你说你正在迭代的集合中的对象是相同的,所以让我们采取类似的东西:

  items = [
    {id:1, name: 'test'},
    {id:1, name: 'test'},
    {id:1, name: 'test'}
  ];

如果你循环上面的数组,Angular会为你创建重复组件的新实例:

<div *ngFor="let item of items">
    <app-object [item]="item"></app-object>
</div>

此处的工作示例: https://stackblitz.com/edit/angular-w8s6qt

如果我错过了什么,请告诉我。

相关问题