自定义指令不起作用

斯托菲尔

我正在尝试为已编辑/私人信息创建指令。如果未提供某些信息,则应显示一个黑匣子(就像内容在那里但被黑匣子隐藏)

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-tag我看不到蓝框

皮埃尔·马勒

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

由于您的标签没有内容,宽度为0,因此您看不到它

如果要强制使用宽度,则必须将其显示设置为inline-block

引用HTML规范

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章