使用* ngFor更改项目数组时如何呈现所有子元素

时间:2019-01-29 16:34:55

标签: angular typescript

我有很多物品。

items = [{id: 0, visible: true, height:56, width: 67 }, 
         {id: 1, visible: true, height:56, width: 67 }, 
         {id: 2, visible: true, height:56, width: 67 }, 
         {id: 3, visible: true, height:56, width: 67} ]

此后,将数组更改为

items = [{id: 0, visible: true,  height:102, width: 67 }, 
         {id: 1, visible: true,  height:102, width: 67 }, 
         {id: 2, visible: false, height:56,  width: 67 }, 
         {id: 3, visible: false, height:56,  width: 67} ]
<div *ngFor="let item of items">
    <child [item]="item"></child>
</div>
items$: Observable<Item[]>;
items: Item[];

this.items$ = this.store.select(state => state.items);

this.items$.subscribe(
    items => {
        if (items.length > 0) {
            this.items = items.filter(x => x.visible === true);
        }
     },
     error => {
         console.log('error', error);
     }
);

预期: items [0],items [1]必须可见,并且div高度宽度必须更改为新值。项目[2],项目[3]需要从dom中删除。

实际: 项目[0],项目[1]可见。但是div的height,width不变。

1 个答案:

答案 0 :(得分:-1)

this.items = items.filter(x => x.visible);您有两个分号不确定是否就是这样。

根据我所见,您没有设置任何样式。您只是将项目传递给我认为可以设置样式的子组件?