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 Answer
Reset to default 21Every 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.
isLogin
. – Pardeep Jain Commented May 15, 2016 at 12:40