Angular 7 ng-if =“”组合条件

时间:2019-05-02 14:21:00

标签: angular multiple-conditions

如何结合Angular 7中的条件?

此代码似乎无效:

<div *ngIf="isSituation1 || (isSituation3 &&  isSituation2)" [@slideInOut]   role="alert">
<h1>
  Show this if either condition is true
</h1>
</div>

在以下情况下,我想显示内容: isSituation1 = true 要么 isSituation3 AND isSituation2 = true

1 个答案:

答案 0 :(得分:0)

模板似乎是正确的:

<!-- app.component.html -->

<div *ngIf="isSituation1 || (isSituation3 &&  isSituation2)" [@slideInOut]   role="alert">
    <h1>Show this if either condition is true</h1>
</div>

JS应该包含以下内容:

// app.component.ts

export class AppComponent implements OnInit {
    isSituation1: boolean = false;
    isSituation2: boolean = false;
    isSituation3: boolean = false;

    // perhaps some logic to check if one of the situations

    ngOnInit() {
        if (someCondition) {
            isSituation1 = true;
        } else if (anotherCondition) {
            isSituation2 = true;
        } else {
            isSituation3 = true;
        }
    }
}

绝对添加一些console.log()语句以检查这些值。 另外,如果isSituation1-3是函数,请将括号添加到html模板isSituation1() || (isSituation2 && isSituation3)中。