问题描述
组件.html
<div class="infoSection" [ngStyle]="FooterStyle"><br>
这里我有一个来自我的 Component.css 的 class(infoSection) 和来自 component.ts 的 [ngStyle]=FooterStyle,我想创建一个条件
<div class="infoSection **(else Run this when Flag===0)**" [ngStyle]="FooterStyle **(RUN THIS IF Flag===1. I dont kNow the Syntax)**"><br>
我已经口头写下了 div 标签中的条件。但我不知道如何做到这一点的确切语法
现在我正在编写来自 component.ts 的 FooterStyle 片段和来自 component.css 的 infoSection 片段
Component.ts
_Flag:any; //Flag Property
//This is Footer Object
FooterStyle={
'background-image':'','background-repeat':'no-repeat','background-size': 'cover','background-attachment': 'fixed','background-position': 'top',}
//Function For Populating the FooterStyle Object with data coming from Backend
this._GeneralSettingsService.GetFooterImage().subscribe((docs:any)=>{
let _docs = docs.Result;
if(_docs){
this.FooterImageUrl = this.FooterImageFolderUrl + _docs[0].imageUrl;
this.FooterStyle['background-image'] = 'url('+this.FooterImageUrl+')';
this._Flag=1;
console.log(this._Flag);
}else{
this._Flag=0;
console.log(this._Flag);
}
})
Component.css
//infoSection snippet
.infoSection{
background-image: url('../../assets/images/tracey2.jpg');
background-size: cover;
background-attachment: fixed;
width:auto;
background-position: center;
}
提前致谢。
解决方法
我相信以前有人问过这个问题,但这里是我将如何在 CSS 类和使用 Angular 的内联样式中实现条件。
<div
[ngClass]="{'infoSection': Flag === 0}"
[ngStyle]="Flag === 1 ? FooterStyle : {}">
<br>
</div>