zhinee
zhinee

Reputation: 135

Angular: Is there a way to stop a component from initializing it's view?

What I am trying to do is, if this.pageService.getForbidden() return true.

I don't want this component to load or initialized, is there a way to achieve this?

export class ViewRComponent extends ViewRulesParentComponent implements OnInit, AfterViewInit {

    constructor( protected readonly pageService: PageService,
               ) {
        if( this.pageService.getForbidden()){
        // this.pageService.getForbidden() is true, I don't want this component to load 
            return;
        }
    }
}

Upvotes: 1

Views: 2174

Answers (2)

Rafael Andrade
Rafael Andrade

Reputation: 505

I don't know how your application is structured, but you can use Angular Guards to prevent the component to be loaded, something like CanActivate and CanLoad guards can help you.

https://angular.io/api/router/CanActivate

https://angular.io/api/router/CanLoad

Upvotes: 0

Panken0
Panken0

Reputation: 60

If you got a parent component you can just use a <div> with *ngIf=pageService.getForbidden()

something like this:

parent.html

<div *ngIf=pageService.getForbidden()>
  <view-r-component></view-r-component>
</div>

where <view-r-component> its your ViewRComponent component

i hope it works for you!

Upvotes: 1

Related Questions