Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.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
Html 为循环*ngFor中的布尔变量赋值_Html_Angular_Ngfor_Angular Ng If - Fatal编程技术网

Html 为循环*ngFor中的布尔变量赋值

Html 为循环*ngFor中的布尔变量赋值,html,angular,ngfor,angular-ng-if,Html,Angular,Ngfor,Angular Ng If,我的组件窗口容器中有一个ngFor循环,其中显示了多个聊天窗口(窗口项)。我想将一个变量(changeColor)设置为true,用于两个div上的一个div,用于两个div上的另一个div。 我希望能够在两个组件中使用此变量:窗口容器和窗口项 这是我在windowcontainer.html中所做的,但它不起作用: <window-item *ngFor="let thread of windows; let i = index" [thread]="thread">

我的组件
窗口容器中有一个ngFor循环,其中显示了多个聊天窗口(
窗口项
)。我想将一个变量(
changeColor
)设置为true,用于两个div上的一个div,用于两个div上的另一个div。 我希望能够在两个组件中使用此变量:
窗口容器
窗口项

这是我在
windowcontainer.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>
和我的
窗口项.scss

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

您可以像这样使用它:

建议您通过
ngFor

偶数:布尔值:当项在iterable中具有偶数索引时为True

奇数:布尔值:当项在iterable中具有奇数索引时为True


//window-item.html
...

您可以像这样使用它:

建议您通过
ngFor

偶数:布尔值:当项在iterable中具有偶数索引时为True

奇数:布尔值:当项在iterable中具有奇数索引时为True


//window-item.html
...

谢谢你的回答,但它不起作用。我没有打开的窗口如何在
窗口容器.component.ts
?@user9099802中创建@input“odd”@Eliseo我在哪里写
@input“odd
”?在
窗口容器.component.ts
?@user9099802,是的“孩子”窗口容器“。我想你是在用@input来传递[thread]:(谢谢你的回答,但它不起作用。我没有打开的窗口如何让@input成为“奇数”@Eliseo我在哪里写
@input”奇数
?在
窗口容器.component.ts
?@user9099802中,是的。我认为您的窗口项是“窗口容器”的“子项”。我认为您正在使用@input传递[thread]:(
<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>