在循环 * ngFor 中为布尔变量赋值
Assign a value to a boolean variable in loop * ngFor
我的组件 window-container
中有一个 ngFor 循环,其中显示了多个聊天 window (window-item
)。我想将一个变量 (changeColor
) 设置为 true,其中一个 div 为两个,另一个 div 为 false。
我希望能够在两个组件中使用此变量:window-container
和 window-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>
我希望变量 changeColor
为每个 div 更改值,以便在我的 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;
}
}
}
你可以这样使用它:
Would suggest you should pass that via odd
/ even
of ngFor
even: boolean: True when the item has an even index in the iterable.
odd: boolean: True when the item has an odd index in the iterable.
<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>
我的组件 window-container
中有一个 ngFor 循环,其中显示了多个聊天 window (window-item
)。我想将一个变量 (changeColor
) 设置为 true,其中一个 div 为两个,另一个 div 为 false。
我希望能够在两个组件中使用此变量:window-container
和 window-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>
我希望变量 changeColor
为每个 div 更改值,以便在我的 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;
}
}
}
你可以这样使用它:
Would suggest you should pass that via
odd
/even
ofngFor
even: boolean: True when the item has an even index in the iterable.
odd: boolean: True when the item has an odd index in the iterable.
<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>