I need to access the path of my url within a *ngIf
. I've tried the following:
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'my-app',
providers: [],
templateUrl: 'app.component.html'
})
export class AppComponent implements OnInit {
constructor(public route: ActivatedRoute) {
}
}
And in my HTML/Pug:
div(*ngIf='route.firstChild?.url?.value[1]?.path == "somePath"')
It does work when I build it using angular cli but when I try to build it with aot, I get the following error: Property 'value' does not exist on type 'Observable<UrlSegment[]>'
Seems like I'm not allowed to directly access the value of an observable inside *ngIf
It's not that you can't directly access the value of the observable, it's that the value of the observable is undefined when the browser initializes the view. This is in general how Observables work since they are asynchronous in nature.
Two ways I can think of to solve this: