*ngIf else if in template

后端 未结 8 779
甜味超标
甜味超标 2020-12-07 11:33

How would I have multiple cases in an *ngIf statement? I\'m used to Vue or Angular 1 with having an if, else if, and else

8条回答
  •  伪装坚强ぢ
    2020-12-07 12:12

    This seems to be the cleanest way to do

    if (foo === 1) {
    
    } else if (bar === 99) {
    
    } else if (foo === 2) {
    
    } else {
    
    }
    

    in the template:

    foo === 1
    
        bar === 99
    
    
        foo === 2
    
    else
    

    Notice that it works like a proper else if statement should when the conditions involve different variables (only 1 case is true at a time). Some of the other answers don't work right in such a case.

    aside: gosh angular, that's some really ugly else if template code...

提交回复
热议问题