繁体   English   中英

在循环中为布尔变量分配一个值* ngFor

[英]Assign a value to a boolean variable in loop * ngFor

我的组件window-container中有一个ngFor循环,其中有几个显示的聊天窗口( window-item )。 我想将一个变量( changeColor )设置为true,将两个div设置为false,将另一个div的false设置为两个。 我希望能够在两个组件中使用此变量: window-containerwindow-item

这是我在window-container.html所做的,但是它不起作用:

<window-item
  *ngFor="let thread of windows; let i = index"
    [thread]="thread">
      <div *ngIf="i % 2 == 0"> {{this.threadService.changeColor = false}}</div>
      <div *ngIf="i % 2 != 0"> {{this.threadService.changeColor = true}}
    </div>
</window-item>

我希望每个div的变量changeColor更改值,以便将其写入我的window-item.html

<div [ngClass]="{'window-grey' : !this.threadService.changeColor, 'window-blue': this.threadService.changeColor}">
    <div class="panel-container">
      <div class="panel panel-default">
        ...
      </div>
    </div>
</div>

和我的window-item.scss

.panel-heading {
   .top-bar {
      .window-grey {
         background: grey;
      }
      .window-grey {
         background: grey;
      }
   }
}

您可以像这样使用它:

建议您通过ngFor odd / even ngFor

even :boolean:当项目在可迭代对象中具有偶数索引时为true。

奇数 :布尔值:当项目在可迭代项中具有奇数索引时为true。

<window-item
  *ngFor="let thread of windows; let i = index ; let odd = odd;"
    [thread]="{ thread : thread , changeColor : odd }">
      <!-- <div *ngIf="i % 2 == 0"> {{this.threadService.changeColor = false}} </div>
      <div *ngIf="i % 2 != 0"> {{this.threadService.changeColor = true}} </div> -->
</window-item>

// window-item.html
<div [ngClass]="{'window-grey' : !changeColor, 'window-blue': changeColor}">
    <div class="panel-container">
      <div class="panel panel-default">
        ...
      </div>
    </div>
</div>

工作演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM