Combine [NgStyle] With Condition (if..else)

前端 未结 10 1427
暗喜
暗喜 2020-12-04 20:55

I have read NgStyle Documentation For Angular 2, and it is a little bit confusing for me. How do I use NgStyle with condition like (if...else) to set background image of any

相关标签:
10条回答
  • 2020-12-04 21:52

    One can also use this kind of condition:

    <div [ngStyle]="myBooleanVar && {'color': 'red'}"></div>
    

    It requires a bit less string concatenation...

    0 讨论(0)
  • 2020-12-04 21:52

    To add and simplify Günter Zöchbauer's the example incase using (if...else) to set something else than background image :

    <p [ngStyle]="value == 10 && { 'font-weight': 'bold' }">
    
    0 讨论(0)
  • 2020-12-04 21:58

    I have used the below code in my existing project

    <div class="form-group" [ngStyle]="{'border':isSubmitted && form_data.controls.first_name.errors?'1px solid red':'' }">
    </div>
    
    0 讨论(0)
  • 2020-12-04 22:02

    Using a ternary operator inside the ngStyle binding will function as an if/else condition.

    <div [ngStyle]="{'background-image': 'url(' + value ? image : otherImage + ')'}"></div>
    
    0 讨论(0)
提交回复
热议问题