最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - why *ngIf in angular 2 always is executing when use function? - Stack Overflow

programmeradmin2浏览0评论

I'm trying to create a application with angular 2,and have a auth service in my application , my html template is somthing like this:

 <header>
    <div *ngIf="isLogin()"><a href="">profile</a></div>
    <div *ngIf="!isLogin()"><a href="">register</a></div>
    <div *ngIf="!isLogin()"><a href="">signin</a></div>
    </header>

**and this is my class :** 

@Component({
    selector: 'main-menu',
    templateUrl: '/client/tmpl/menu.html',
    directives: [ROUTER_DIRECTIVES]
})
export class Menu extends Ext {

    public items: any;

    constructor(private _util: UtilService, private _user: UserService) {
        super();


    }

    public isLogin() {

        console.log("test");  <==== my problem is here
        return this._user.authorized();

    }


}

always my functions is executing !(in my auth service i have another fuctions that they also runing) !this is for using a function inside *ngif ??!!! im worry for my resoureces and i want know its a problem or not?

I'm trying to create a application with angular 2,and have a auth service in my application , my html template is somthing like this:

 <header>
    <div *ngIf="isLogin()"><a href="">profile</a></div>
    <div *ngIf="!isLogin()"><a href="">register</a></div>
    <div *ngIf="!isLogin()"><a href="">signin</a></div>
    </header>

**and this is my class :** 

@Component({
    selector: 'main-menu',
    templateUrl: '/client/tmpl/menu.html',
    directives: [ROUTER_DIRECTIVES]
})
export class Menu extends Ext {

    public items: any;

    constructor(private _util: UtilService, private _user: UserService) {
        super();


    }

    public isLogin() {

        console.log("test");  <==== my problem is here
        return this._user.authorized();

    }


}

always my functions is executing !(in my auth service i have another fuctions that they also runing) !this is for using a function inside *ngif ??!!! im worry for my resoureces and i want know its a problem or not?

Share Improve this question asked May 15, 2016 at 12:29 user5738822user5738822 4
  • 1 what you want to do exactly ? obviously your function has to call every time because it is bind with binding on view so every time angular check for any change in the isLogin. – Pardeep Jain Commented May 15, 2016 at 12:40
  • This is like setinterval or a infinite loop!! – user5738822 Commented May 15, 2016 at 15:46
  • Please can you reformulate your post, it's not really understandable. – Romain Commented May 15, 2016 at 15:53
  • hmm as already said angular will always call the function for binding checking thts y function is being called like infinite loop\ – Pardeep Jain Commented May 15, 2016 at 15:56
Add a comment  | 

1 Answer 1

Reset to default 21

Every time Angulars change detection is run, it evaluates all bindings and therefore calls your functions to check if the view needs updating.

Using functions in bindings is discouraged. Either assign the value to a property of your component class and bind to this property instead, or use observables and the | async pipe to notify Angular about changed values.

Another option is to use ChangeDetectionStrategy.OnPush where Angular change detection is only run when an input value has changed.

发布评论

评论列表(0)

  1. 暂无评论