如何在 *ngIf HTML 中获取 .ts var 值?
How to get .ts var value in *ngIf HTML?
我想显示所有 2 秒的图像,我设法先显示它们。
这是我的 HTML 行:
<img id="bh" routerLink="/" *ngIf="bh?.id == count" [src]="bh?.src" height="42" width="42"/>
还有我的 .ts 方法:
Timer(){
setInterval(() => {
this.count=this.count++
}, 2000)
}
与:
export class HeaderComponent implements OnInit{
count=0
BHs = [
new Bh (1, "assets/img/photo_bonhommes/BH1.jpg.png"),
new Bh (2, "assets/img/photo_bonhommes/BH2.png"),
new Bh (3, "assets/img/photo_bonhommes/BH3.png"),
new Bh (4, "assets/img/photo_bonhommes/BH4.png"),
new Bh (5, "assets/img/photo_bonhommes/BH5.png"),
new Bh (6, "assets/img/photo_bonhommes/BH6.png"),
new Bh (7, "assets/img/photo_bonhommes/BH7.png"),
new Bh (8, "assets/img/photo_bonhommes/BH8.png"),
new Bh (9, "assets/img/photo_bonhommes/BH9.png"),
new Bh (10, "assets/img/photo_bonhommes/BH10.png"),
new Bh (11, "assets/img/photo_bonhommes/BH11.png"),
];
}
顺便说一下,它是用 *ngFor 显示的,但我想我必须在这里使用 *ngIf..
我也试过了:
<img id="bh" routerLink="/" *ngIf="bh?.id == this.count" [src]="bh?.src" height="42" width="42"/>
但没有用..非常感谢!
你的代码大部分看起来不错,我调整了一些地方。唯一可能出现的问题是图片路径错误,它可能应该以“/assets/img/photo_bonhommes/BH1.jpg”开头,绝对不应该有两个扩展名。
另一个是您的 Bh class 构造函数创建了 class 但您没有正确分配和公开 id。
export class HeaderComponent implements OnInit{
count=0
BHs = [
new Bh(1, "/assets/img/photo_bonhommes/BH1.jpg"),
new Bh(2, "/assets/img/photo_bonhommes/BH2.png"),
new Bh(3, "/assets/img/photo_bonhommes/BH3.png"),
new Bh(4, "/assets/img/photo_bonhommes/BH4.png"),
new Bh(5, "/assets/img/photo_bonhommes/BH5.png"),
new Bh(6, "/assets/img/photo_bonhommes/BH6.png"),
new Bh(7, "/assets/img/photo_bonhommes/BH7.png"),
new Bh(8, "/assets/img/photo_bonhommes/BH8.png"),
new Bh(9, "/assets/img/photo_bonhommes/BH9.png"),
new Bh(10, "/assets/img/photo_bonhommes/BH10.png"),
new Bh (11, "/assets/img/photo_bonhommes/BH11.png"),
];
}
ngOnInit() {
Timer(){
setInterval(() => {
this.count++
}, 2000)
}
}
--------------------HTML----------------
<ng-container *ngFor="let bh of BHs">
<img id="bh" routerLink="/" *ngIf="bh.id === count" [src]="bh.src" height="42"
width="42"/>
</ng-container>
我想显示所有 2 秒的图像,我设法先显示它们。
这是我的 HTML 行:
<img id="bh" routerLink="/" *ngIf="bh?.id == count" [src]="bh?.src" height="42" width="42"/>
还有我的 .ts 方法:
Timer(){
setInterval(() => {
this.count=this.count++
}, 2000)
}
与:
export class HeaderComponent implements OnInit{
count=0
BHs = [
new Bh (1, "assets/img/photo_bonhommes/BH1.jpg.png"),
new Bh (2, "assets/img/photo_bonhommes/BH2.png"),
new Bh (3, "assets/img/photo_bonhommes/BH3.png"),
new Bh (4, "assets/img/photo_bonhommes/BH4.png"),
new Bh (5, "assets/img/photo_bonhommes/BH5.png"),
new Bh (6, "assets/img/photo_bonhommes/BH6.png"),
new Bh (7, "assets/img/photo_bonhommes/BH7.png"),
new Bh (8, "assets/img/photo_bonhommes/BH8.png"),
new Bh (9, "assets/img/photo_bonhommes/BH9.png"),
new Bh (10, "assets/img/photo_bonhommes/BH10.png"),
new Bh (11, "assets/img/photo_bonhommes/BH11.png"),
];
}
顺便说一下,它是用 *ngFor 显示的,但我想我必须在这里使用 *ngIf..
我也试过了:
<img id="bh" routerLink="/" *ngIf="bh?.id == this.count" [src]="bh?.src" height="42" width="42"/>
但没有用..非常感谢!
你的代码大部分看起来不错,我调整了一些地方。唯一可能出现的问题是图片路径错误,它可能应该以“/assets/img/photo_bonhommes/BH1.jpg”开头,绝对不应该有两个扩展名。 另一个是您的 Bh class 构造函数创建了 class 但您没有正确分配和公开 id。
export class HeaderComponent implements OnInit{
count=0
BHs = [
new Bh(1, "/assets/img/photo_bonhommes/BH1.jpg"),
new Bh(2, "/assets/img/photo_bonhommes/BH2.png"),
new Bh(3, "/assets/img/photo_bonhommes/BH3.png"),
new Bh(4, "/assets/img/photo_bonhommes/BH4.png"),
new Bh(5, "/assets/img/photo_bonhommes/BH5.png"),
new Bh(6, "/assets/img/photo_bonhommes/BH6.png"),
new Bh(7, "/assets/img/photo_bonhommes/BH7.png"),
new Bh(8, "/assets/img/photo_bonhommes/BH8.png"),
new Bh(9, "/assets/img/photo_bonhommes/BH9.png"),
new Bh(10, "/assets/img/photo_bonhommes/BH10.png"),
new Bh (11, "/assets/img/photo_bonhommes/BH11.png"),
];
}
ngOnInit() {
Timer(){
setInterval(() => {
this.count++
}, 2000)
}
}
--------------------HTML----------------
<ng-container *ngFor="let bh of BHs">
<img id="bh" routerLink="/" *ngIf="bh.id === count" [src]="bh.src" height="42"
width="42"/>
</ng-container>