【问题标题】:How to show/hide element in *ngFor array, without touching this Array?如何在 *ngFor 数组中显示/隐藏元素,而不触及该数组?
【发布时间】:2016-06-13 12:14:21
【问题描述】:

1.你好。关于如何显示/隐藏元素有很多答案,为迭代数组的每个元素附加一个我们在真/假时更改的属性。

 <button  (click)="attachmentHide = !attachmentHide"></button>
<itemComp [itemsArray]='item.children' class="col-sm-offset-1 subItemHiden" [class.subItemsShow] = 'showSubItem'></itemComp>

所以每次我们列出数组的每个元素时,它都会创建 item.showSubItem 属性。在这种情况下,我们的数组已更改。

但是如果我想显示/隐藏这些项目,但不编辑主数组,该怎么办。这很重要,因为我在开始时检查了这个数组是否相等。 有创建与数组无关的 var 的答案,但是如何为每个元素创建 var 以分别显示/隐藏每个元素?

更新:

<div class="row col-sm-offset-1"  *ngFor="let item of itemsArray">
    <div class="col-sm-4">
        <div class="row assignedItem">
            <span class="glyphicon glyphicon-plus-sign " title="sub items"  (click)= "showSubItem = !showSubItem"></span>

            <div class=" itemName" title="State:{{item.state}},Type: {{item.type}}">{{item.name}}</div>
            <!--users component-->

        <!--show subItems-->
        <itemComp [itemsArray]='item.children' class="col-sm-offset-1 subItemHiden" [class.subItemsShow] = 'showSubItem'></itemComp>
    </div>
</div>

ts 文件:

export class ItemComponent {
@Input() itemsArray: Array<Object>;

constructor () {}
}

更新 2:

我在 home.ts 中用 i=false 初始化了数组:

viewNodes(result) {
    setTimeout(() => {
        this.myRes =  result;
        this.showSubItemsArr = this.myRes.content.map(i => false);
        this.itemsParentArray = this.myRes.content;
        console.log( this.showSubItemsArr );
        this.showAssigned = true;
    }, 3000);
}

在我将它发送到子组件后,在 html 中:

<div class="container">
<div class="row">
    <!--show item-->
    <itemComp [showSubItems]="showSubItemsArr" [itemsArray]='itemsParentArray'  ></itemComp>
</div>

我尝试使用 showSubItems[idx] 查看项目。

    <div class="row col-sm-offset-1"  *ngFor="let item of itemsArray let idx=index">
    <div class="col-sm-4">
        <div class="row assignedItem">
            <span class="glyphicon glyphicon-plus-sign " title="sub items"  (click)= "showSubItem = !showSubItem"></span>
            <span class="glyphicon glyphicon-paperclip" title="attached docs" (click)="attachmentHide = !attachmentHide"></span>
            <div class=" itemName" title="State:{{item.state}},Type: {{item.type}}">{{item.name}}</div>
            <!--users component-->
            <usersComp [userArray]="item.assignment"></usersComp>
        </div> {{showSubItems}}
        <!--attachment component-->
        <attachmentComp class="col-sm-offset-1" [attachmentsArray]='item.attachments' *ngIf="attachmentHide"></attachmentComp>
        <!--show subItems-->
        <itemComp [itemsArray]='item.children' class="col-sm-offset-1 subItemHiden" [class.subItemsShow] = 'showSubItems[idx]'></itemComp>
    </div>
</div>

但它显示错误:

TypeError: 无法读取未定义的属性“0”

但是当我渲染所有 {{showSubItems}} 数组时,它会毫无问题地显示 false,false,false。

看起来像idx 值还没有准备好进行第二次迭代。抱歉不能使用 plunker(正在处理它)。

【问题讨论】:

  • 你的*ngFor在哪里?
  • 我已经更新了。谢谢。
  • 您没有按照我在回答 ((click)= "showSubItems[idx] = !showSubItems[idx]") 中显示的方式更新点击处理程序。是点击按钮前的错误吗?
  • setTimeout(...) 看起来很糟糕,而且是不好的做法。你需要它做什么?尝试设置showSubItems to an initial value like showSubItems = [];`
  • 如果ngOnChanges() 适合你,你可以试试。每次更新 @Input() 时都会调用它(通过 Angular 绑定,而不是在代码直接更新属性时)。注意:在将数组传递给绑定 [someProp]="someArray" 后修改数组时,不会调用 ngOnChanges()。

标签: angular ngfor


【解决方案1】:

当this.itemsArray 数据可用时,我们创建另一个数组showSubItems,它为this.itemsArray 中的每个项目获取一个条目(默认为false):

constructor() { // or somewhere else where `this.itemsArray` data is already set
  this.showSubItems = this.itemsArray.map(i => false);
}

我们使用ngFor 的index 特性并声明一个idx 变量。使用此变量,我们引用 this.showSubItems 数组项中的项,其索引与来自this.itemsArray 的项相同

<div class="row col-sm-offset-1"  *ngFor="let item of itemsArray let idx=index">
    <div class="col-sm-4">
        <div class="row assignedItem">
            <span class="glyphicon glyphicon-plus-sign " title="sub items"  (click)= "showSubItems[idx] = !showSubItems[idx]"></span>

            <div class=" itemName" title="State:{{item.state}},Type: {{item.type}}">{{item.name}}</div>
            <!--users component-->

        <!--show subItems-->
        <itemComp [itemsArray]='item.children' class="col-sm-offset-1 subItemHiden" [class.subItemsShow] = 'showSubItems[idx]'></itemComp>
    </div>
</div>

【讨论】:

  • 唯一的问题:我已经从 'rxjs/Observable' 导入了 import {Observable};但浏览器显示:TypeError:无法读取未定义的属性“地图”。会是什么?
  • 如前所述,this.showSubItems = this.itemsArray.map(i =&gt; false); 将在设置this.itemsArray 时执行。如果是undefined,则在执行this.itemsArray.map(...) 时会出现此错误。从您的问题中我看不到this.itemsArray 的设置位置,因此我以构造函数为例。
  • 如果您将某些内容传递给输入[xxx]="...",则在创建组件时xxx 不会立即可用(仅在调用ngOnChanges 或ngOnChanges 之后),尤其是当您使用@ 987654341@。你需要提防showSubItems还没有设置像[class.subItemsShow] = 'showSubItems &amp;&amp; showSubItems[idx]'
猜你喜欢
  • 2019-05-31
  • 1970-01-01
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多