在* ngIf中使用函数运行几次而不是一次 [英] using a function in *ngIf runs several times instead of once

查看:395
本文介绍了在* ngIf中使用函数运行几次而不是一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre *ngIf="isAdmin()">{{email|json}} - {{user|json}}</pre>

组件

isAdmin() {
    console.log('isAdmin: ', this.bcAuthService.isAdmin());
    return this.bcAuthService.isAdmin();
}

服务

isAdmin() {
    return this.admins.includes(localStorage.getItem("email"));
}

问题

组件中的功能使打印多次.为什么?这是错的吗?有什么更好的方法?

Problem

the function in the component keeps printing several times. Why? Is this wrong? What is a better way?

推荐答案

-executing-when-use-function>为什么在使用功能时* 2中的* ngIf总是在执行?

after finding this why *ngIf in angular 2 always is executing when use function?

我通过

ngOnInit() {
    this.is_admin();
}
is_admin() {
    this.isAdmin = this.bcAuthService.isAdmin();
}

html

<pre *ngIf="isAdmin">{{email|json}} - {{user|json}}</pre>

这篇关于在* ngIf中使用函数运行几次而不是一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆