When implementing the code below to toggle the class, everything runs smoothly
<p [ngClass]="idPresent ? 'alert alert-success' : 'alert alert-danger'">
Working
</p>
After changing the value of IdPresent
in the component, I observe the correct class being applied,
However, when I use an object in the component for the same purpose, it only functions properly if idPresent
is set to false
. If I switch idPresent
to true
, the code provided fails to apply the right class.
<p [ngClass]="idValueClass">
Not working
</p>
public idPresent = false;
public idValueClass = {
"alert alert-success" : this.idPresent,
"alert alert-danger" : !this.idPresent
};
Could someone assist me in understanding what mistake I might be making?
Update:
I modify the value of idPresent once I receive a response from the REST API call
ngOnInit(): void {
this.http.get('http://localhost:8083/v1/uniqueid', {
headers: {'Authorization':'Basic dXNlcjpwYXNzd29yZA=='}
})
.subscribe(response => {
if(response) {
console.log(response)
this.serverId = response;
this.idPresent = true;
} else {
this.idPresent = false;
}
console.log("idPresent : " + this.idPresent)
})
}
Once the server responds, I can observe the updated value in the console.