Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
几秒钟后显示和隐藏多个div angular 2/typescript_Angular_Typescript_Angular5_Angular2 Template - Fatal编程技术网

几秒钟后显示和隐藏多个div angular 2/typescript

几秒钟后显示和隐藏多个div angular 2/typescript,angular,typescript,angular5,angular2-template,Angular,Typescript,Angular5,Angular2 Template,基本上,我通过传递一些数据来创建多个div。并试图根据某些条件隐藏相同的div 它正在创建多个div,但它隐藏所有div,而不考虑任何条件 如果我为一个div传递“on:true”,它会隐藏所有div。这可能是因为我在所有元素上运行的forEach循环。但不知道如何修复它 这是我的打字脚本代码--> 导出类ShowDataComponent实现OnInit{ @ViewChildren(“通知”)私有el:QueryList; this.showData=arrayData; ngAfterV

基本上,我通过传递一些数据来创建多个div。并试图根据某些条件隐藏相同的div

它正在创建多个div,但它隐藏所有div,而不考虑任何条件

如果我为一个div传递“on:true”,它会隐藏所有div。这可能是因为我在所有元素上运行的forEach循环。但不知道如何修复它

这是我的打字脚本代码-->

导出类ShowDataComponent实现OnInit{
@ViewChildren(“通知”)私有el:QueryList;
this.showData=arrayData;
ngAfterViewInit(){
for(设i=0;i{
const HtmleElement=element.nativeElement作为HtmleElement;
setTimeout(()=>htmlElement.setAttribute(“样式”,“显示:无;”),this.duration);
});
}
}
}

}
它隐藏所有div,因为您将
forEach
放在
if(this.on)之后{
这意味着它只需要showData数组中的一项就可以将上的属性设置为true。

首先,你不应该自己处理DOM,这是框架的工作。如果你不使用框架,拥有一个框架有什么意义

现在我不理解您的代码,因为您没有在类成员上声明一个
,并且您的代码被用来隐藏所有div

因此,我假设元素上有一个条件,称为
on,因为它似乎与您所期望的最接近

从HTML开始:

<div *ngFor="let n of showData">
  <div *ngIf="n.on" class="show-top-bar" style="display: block;">
      {{n.message}}
  </div>
</div>
最后,在超时后将所有属性设置为其初始值:

ngAfterViewInit() {
  setTimeout(() => {
    for (let n of this.showData) {
      n.on = this.referenceArray.find(rn => rn.id === n.id).on;
    }
  });
}
ngAfterViewInit() {
  setTimeout(() => {
    for (let n of this.showData) {
      n.on = this.referenceArray.find(rn => rn.id === n.id).on;
    }
  });
}