I want to apply a ngIf
directive to a DOM element, this ngIf
watch a function and that function check the values of other variables. As the following:
export class Controller{
private x: ObjectClass;
public isDone():boolean{
return this.x.isY() && this.otherFunction();
}
private otherFunction():boolean{
let result:boolean = true;
this.array.forEach((value)=>{
if(!value){
result = false;}
});
return result
}
}
//define the state
.state('app.first-state', {
url: '/first-state,
views: {
'renderView': {
template: require('./states/first-state.html'),
controller: 'Controller as Ctrl',
}
}
})
//in the first-state.html
<div ng-if="Ctrl.isDone()"></div>
x
is an object who is carrying methods and variables.
The x.isY()
and this.otherFunction()
change its return values to true
but the ngIf does not recreate the DOM element. It seems that the digest cycle doesn't watch the x
object neither array
object. Which makes sense since I am not using them directly into DOM elements. However, the method isDone()
runs only when I get into the state or I get out from the state.
I did the following workaround, but I am concerned that there will be a performance issue:
private isDoneVar:boolean = false;
//then make my "own digest cycle"
$interval(() => {
this.isDoneVar = this.isDone();
}, 700);
//and in the div
<div ng-if="Ctrl.isDoneVar"></div>
I have two questoins:
- Why does the
isDone
function run when I enter and leave the state? - Is there any better way to tell angular to run this function each digest cycle run? Or to watch any changes for its internal variables(without adding
$watch
to each variable)