代码之家  ›  专栏  ›  技术社区  ›  Stoffel

自定义指令不工作

  •  1
  • Stoffel  · 技术社区  · 7 年前

    我正试图为编辑/私人信息创建一个指令。 如果未提供某些信息,则应显示一个黑匣子(就像内容在那里,但用黑匣子使其不可见一样)

    import { Directive, ElementRef, Renderer, OnInit } from 
    '@angular/core';
    
    @Directive({
    selector: '[appRedactedContent]'
    })
    export class RedactedContentDirective implements OnInit {
    min = 75;
    max = 150;
    width = this.randomIntFromInterval(this.min, this.max);
    constructor(private el: ElementRef,
              private renderer: Renderer) {
              }
    ngOnInit() {
      this.renderer.setElementStyle(
            this.el.nativeElement, 'background-color', 'blue !important');
            this.renderer.setElementStyle(this.el.nativeElement, 'width', 
                                          this.width.toString());
      }
    
    randomIntFromInterval(min: number, max: number): number {
     return Math.floor(Math.random() * (max - min + 1) + min);
    }
    }
    

    html

    <a appRedactedContent></a>
    

    打开开发人员工具时,我可以看到添加的样式,但a标签在浏览器中看不到蓝色框

    1 回复  |  直到 7 年前
        1
  •  1
  •   Pierre Mallet    7 年前

    标记作为内联的默认显示,不能强制为宽度或高度。

    由于a标记没有内容,因此将其宽度设为0,然后您将看不到它

    如果要强制宽度,必须将其显示设置为“内联块”

    ref to HTML specs