使用角度4显示基于查询参数的菜单项

时间:2017-12-01 09:44:39

标签: angular angular-ng-if ngif

我正在处理基于角色的菜单,以根据查询字符串显示菜单项。就像当网址是http://www.something.com/something?type=admin形式时,我必须只显示管理员的菜单项。但我的逻辑是隐藏所有菜单项。因为我已经在我的 header.component.ts

中实现了逻辑
ngOnInit() {
    this.route.queryParams
    .filter(params => params.type)
    .subscribe(params => {
        console.log(params); //{type: "admin"}
        this.type = params.type;
        console.log(this.type);

        if(this.type == "admin"){
            console.log("ADMIN PAGE");                
                //this.router.navigate(['dashboard'], { queryParams: { type: this.type } });
                //this.router.navigate(['dashboard']);
        }else if(this.type == "national"){
            console.log("NATIONAL PAGE");
        }else if(this.type == "warrington"){
            console.log("WARRINGTON PAGE");
        }else if(this.type == "subcontractorAdmin"){
            console.log("SUB CONTRACOTR ADMIN PAGE");
        }else if(this.type == "subcontractorManager"){
            console.log("SUB CONTRACTOR MANAGER PAGE");
        }else{
            console.log("OTHERS PAGE");
        }
    });
  }

当我使用逻辑时,它的工作正常如预期。但是当我尝试显示和隐藏基于* ngIf =" type = admin"的菜单项时,会发生什么?然后所有的菜单项都被隐藏了。请参阅下面的模板代码。

header.component.html

<ul class="nav navbar-nav">
              <li routerLinkActive="active" *ngIf="'type' == 'admin';">
                <a href="javascript:void(0);" (click)="navigateExternal('http://www.something.com/dashboard','_self')">Dashboard</a>
              </li>
              <li routerLinkActive="active" *ngIf="'type' == 'admin';">
                  <a href="javascript:void(0);" (click)="navigateExternal('http://www.something.com/supplier-list','_self')">Suppliers</a>
              </li>
              <li routerLinkActive="active" *ngIf="'type' == 'admin';">
                  <a href="javascript:void(0);" (click)="navigateExternal('http://www.something.com/compliance-supplier-list','_self')">Compliance</a>
              </li>
              <li routerLinkActive="active" *ngIf="'type' == 'admin';">
                  <a href="javascript:void(0);" (click)="navigateExternal('http://www.something.com/admin-invoice-list','_self')">Invoicing</a>
              </li>
              <li routerLinkActive="active" *ngIf="'type' == 'admin';">
                  <a href="javascript:void(0);" (click)="navigateExternal('http://www.something.com/internal-user-list','_self')">User Management</a>

              </li>
              <li routerLinkActive="active" *ngIf="'type' == 'admin';">
                  <a href="javascript:void(0);" (click)="navigateExternal('http://www.something.com/node/add/upload_weight_csv','_self')">Weight Ticket</a>

              </li>
              <li routerLinkActive="active" *ngIf="'type' == 'admin';">
                <a routerLink="/searchsupplier">Search</a>
              </li>
            </ul>

任何人都可以帮我解释为什么所有菜单项都隐藏起来。

1 个答案:

答案 0 :(得分:1)

我认为你应该改变这条线

 <li routerLinkActive="active" *ngIf="'type' == 'admin';">

 <li routerLinkActive="active" *ngIf="type == 'admin'">

这与名为&#39; type&#39;的变量相比较。而不是硬编码字符串'type',并删除*ngIf末尾不必要的分号,因为那里不需要。