mercredi 2 octobre 2019

Typescript if condition with boolean not working properly

There is something strange going on and I really don't know why.
In my angular project I have a component which has a child-component. The parent component's html is like this:

<child-component [isEditable]="isEditable"></child-component>

In parent component ts I declared the property "isEditable" :

isEditable: boolean = true;

and then in ngOnInit:

this.isEditable = false;

Then in child component I have the correspondent input property with default value:

@Input() isEditable: boolean = true;

When this value changes to false from the parent, I need to implement some logic. The problem is that both in ngOnInit and ngOnChanges of the child component, if I do

if(!this.isEditable) {
   console.log("DO SOMETHING");
} 

It doesn't enter the condition. So I thought about some "delay" in receiving the new value, added a console.log before the if condition but it turned out it was correctly false.
I ended up doing this in ngOnInit method:

console.log("BEFORE IF", this.isEditable);
if(this.isEditable){
   console.log("ENTERED IN TRUE", this.isEditable);
}

and this is the output:

enter image description here

I really don't get what's wrong. I'm sure it's something stupid but I don't get it...

Aucun commentaire:

Enregistrer un commentaire