Issue
Can *ngIf
only check for local variables of the page or can I check global variables?
I want to do the following:
home.page.html
<div *ngIf="debugService.debugActive"></div>
debug.service.ts
debugActive: boolean = true;
contructor() {}
...
But right now, I have to first initialize the variable on my home.page.ts in order to use it in the html:
home.page.ts
import { DebugService } from 'src/app/services/debug.service';
localDebugActive: boolean = false;
constructor(
private debugService: DebugService,
) {}
ngOnInit() {
this.localDebugActive = this.debugService.debugActive;
}
home.page.html
<div *ngIf="localDebugActive"></div>
This means an extra-step and filling my whole pages with local variables. Is it possible without redeclaring it and using it directly in the HTML?
Solution
There are two way:
Make a service instance public
constructor( public debugService: DebugService, ) {}
Use a method (recommended way).
public isDebugActive() { return this.debugService.debugActive; } <div *ngIf="isDebugActive()"></div>
Answered By - Jitesh Yadav
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.