angular - ngIf 语句在 Ionic 4 中执行多次

标签 angular ionic-framework ionic4

我正在使用 ionic 4,但在使用 ngIf 指令时遇到问题。问题是 ngIf 指令语句被执行了多次。注意 *ngIf="isLoggedIn()"

我在 tab3.page.html 中有这个

<ion-header>
  <ion-toolbar>
    <ion-title>
      Tab Three
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>
  <ion-card>
    <div *ngIf="isLoggedIn()">
      <div class="ion-padding">
        <ion-button expand="block" type="submit" class="ion-no-margin" routerLink="/tabs/tab3/register">Sign up</ion-button>
        <br>
        <ion-button expand="block" type="submit" class="ion-no-margin" routerLink="/tabs/tab3/login">Log in</ion-button>
      </div>
    </div>
    <!-- <div *ngIf="isLoggedIn()">
      You are logged in
    </div> -->
    <p *ngIf="isTrue()">
      Paragraph. True.
    </p>
  </ion-card>
</ion-content>

我在 tab3.page.ts

中有这个
isLoggedIn(): boolean {
    console.log('isLoggedIn called');
    let logged: boolean = false;
    return logged;
    // this.authService.loggedIn()
    // .then( val => {
    //   logged = val;
    // });
    // return logged;
  }

  isTrue() {
    console.log('isTrue() called');
    return true;
  }

在浏览器控制台中,这是输出

enter image description here

谁能解释这种通过 ngIf 指令多次调用 isLoggedIn()isTrue() 的行为?

最佳答案

Structural directives are responsible for HTML layout. They shape or reshape the DOM's structure, typically by adding, removing, or manipulating elements.

Structural directives

*ngIf 每次都会检查您传入的数据是真还是假。因此,在您的情况下,它多次调用 isLoggedIn()isTrue() 来检查结果是否已更改。

关于angular - ngIf 语句在 Ionic 4 中执行多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59629380/

相关文章:

angular - Ionic 4 - 平板电脑上的点击与触摸启动(点击/移动配置问题)

Angular 不断自动重新加载

java - 对 Spring Boot 的 Angular HTTP POST 请求导致空值

javascript - 如何在 Angular 4 中执行 onkeypress

android - 404 未找到 - 仅来自移动设备

javascript - ionic 菜单创建自己的类型或更改现有的动画和行为

javascript - 期望验证器返回 Promise 或 Observable

ios - 在 Ionic Cordova 应用程序中,iframe 中的跨源内容中的外部超链接可在 Android 上运行,但在 iOS 上不起作用

css - 如何在 Ionic 2 中将文本换行到 <ion-segment-button> 中

android - 错误 : native-run was not found on your PATH.( ionic 4)