简体   繁体   English

使用指令隐藏角材料垫标签

[英]Hide angular material mat-tab using directive

I am using angular material tabs and I want to use directiveto hide tab element dynamically.我正在使用角材料选项卡,我想使用指令动态隐藏选项卡元素。

html template html模板

<mat-tab-group>
  <!-- how to hide this using directive? -->
  <mat-tab appHideMe label="First"> Content 1 </mat-tab>
  <!-- like this -->
  <mat-tab *ngIf="false" label="Second"> Content 2 </mat-tab>
  <mat-tab label="Third"> Content 3 </mat-tab>
</mat-tab-group>

<div appHideMe>
  hidden
</div>
<div>
  visible
</div>

HideMe directive HideMe指令

    @Directive({
      selector: '[appHideMe]'
    })
    export class HideMeDirective implements AfterViewInit {
      constructor(
        private el: ElementRef
      ) { }
    
      ngAfterViewInit() {
        this.el.nativeElement.style.display = 'none';
      }
    }

As during compilation, mat-tab is replaced so display = 'none' will not work.在编译期间, mat-tab被替换,因此display = 'none'将不起作用。 Is there any way how to hide mat-tab like *ngIf does (using HideMeDirective )?有什么方法可以像*ngIf那样隐藏mat-tab吗(使用HideMeDirective )?

Here is a stackblitz example. 是一个堆栈闪电战示例。


I also want mat-tab to be toggleable.我还希望mat-tab可以切换。 In this example I expect third to be visible but it is not.这个例子中,我希望third是可见的,但它不是。

template模板

<mat-tab-group>
  <!-- how to hide this using directive? -->
  <div>
    <mat-tab label="First"> Content 1 </mat-tab>
  </div>
  <div appHideMe="hide">
    <mat-tab label="Second"> Content 2 </mat-tab>
  </div>
  <div appHideMe>
    <mat-tab label="Third"> Content 3 </mat-tab>
  </div>
  <div>
    <mat-tab label="Fourth"> Content 4 </mat-tab>
  </div>

</mat-tab-group>

<div appHideMe>
  hidden
</div>
<div>
  visible
</div>

directive code指令代码

    @Directive({
      selector: '[appHideMe]'
    })
    export class HideMeDirective implements AfterViewInit {
    
      @Input() appHideMe: string;
    
      constructor(
        private el: ElementRef
      ) { }
    
      ngAfterViewInit() {
        
        if (this.appHideMe === 'hide') {
          this.el.nativeElement.style.display = 'none';
        }
        // should be displayed
        // this.el.nativeElement.style.display = 'none';
      }
    }

As long as there is no HideMeDirective on div, mat-dat will be displayed.只要div上没有HideMeDirective ,就会显示mat-dat When HideMeDirective is added (see third mat-tab ), element is not visible.添加HideMeDirective (参见第三个mat-tab ),元素不可见。 Any ideas?有任何想法吗?

Try Something like this尝试这样的事情

Define one variable定义一个变量

import { Directive, ElementRef, AfterViewInit,ChangeDetectorRef } from '@angular/core';   
@Directive({
    selector: '[appHideMe]',
    exportAs:'appHideMe',  
})
    
export class HideMeDirective implements AfterViewInit {   
    hide:Boolean;    
    constructor(
        private el: ElementRef,
        private cdr:ChangeDetectorRef
    ) { }
    
    ngAfterViewInit() {
        this.hide=false;
        this.cdr.detectChanges();
    }
}

Then use template ref然后使用模板引用

<mat-tab appHideMe #ref="appHideMe" label="First"> Content 1 </mat-tab>
<mat-tab *ngIf="ref.hide" label="Second"> Content 2 </mat-tab>

Example: https://stackblitz.com/edit/angular-mqc1co-vlw9aa示例: https : //stackblitz.com/edit/angular-mqc1co-vlw9aa

<mat-tab> is just another directive generating more code that you don't see before executing. <mat-tab>只是另一个指令,它会生成更多您在执行前看不到的代码。 You have to hide a specific div that gets an id depending on the quantity of tabs.您必须隐藏一个特定的 div,该 div 会根据选项卡的数量获取 id。

在此处输入图片说明

That is why your hide-directive on the <mat-tab> directive does not work.这就是为什么<mat-tab>指令上的隐藏指令不起作用的原因。

You have to write a directive that targets these elements using the classes as selector.您必须使用类作为选择器编写一个针对这些元素的指令。

this.el.nativeElement.children
   .find(child => child.querySelector('.mat-tab-label') !== null)
.style.display == "none"; // there might be a better solution than that

What about this:那这个呢:

<mat-tab-group>
  <mat-tab appHideMe label="First"> Content 1 </mat-tab>
<div appHideMe>
 <mat-tab label="Second"> Content 2 </mat-tab>
</div>

  <mat-tab label="Third"> Content 3 </mat-tab>
</mat-tab-group>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM