byCoder
byCoder

Reputation: 9184

AngularJS 2: new router and templateProvider practice from Angular 1

I'm new to angular 2. In angular 1 i used ui-router and templateProvider to use different templates in one controller, for example:

     templateProvider: function ($localStorage) {
      if($localStorage.isAdmin) { //just an example, here could be any condition, like url has some params
        return '<admin></admin>';
      } else if($localStorage.isManager) {
        return '<manager></manager>';
      }
    },

how can i achieve something similar in angular 2?

for example i have such routes:

{ path: 'customers/:id', component: CustomerDetailsComponent },
{ path: 'customers/:id/edit', component: CustomerDetailsComponent }

how can i in CustomerDetailsComponent check it?

like:

  ngOnInit(): void {
    this.activatedRoute.params.subscribe(params => {
      if (params['edit']) { //how to add a condition? to split edit from show view
        /*someTemplateLogic*/
      }
    });
  }

is it possible to do?

so: two different templates for routes:

{ path: 'customers/:id', component: CustomerDetailsComponent },
{ path: 'customers/:id/edit', component: CustomerDetailsComponent }

with edit and without - how to check which route i have, and set such template in one component

Upvotes: 2

Views: 103

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657376

<admin *ngIf="isAdmin"></admin
<manager *ngIf="!isAdmin"></manager>
  ngOnInit(): void {
    this.router.events
    .filter(f => f instanceof NavigationEnd)
    .forEach(e => this.isAdmin = e.url.endsWith('/edit'));
  }

Upvotes: 1

Related Questions