简体   繁体   English

为什么类不适用于Angular指令?

[英]Why don't classes apply to Angular directives?

I'm a little confused how Angular directives are supposed to render as DOM elements. 我有点困惑Angular指令应该如何呈现为DOM元素。

My directive template elements take classes fine, and they render at the correct dimensions, regardless of the directive element's dimensions. 我的指令模板元素使类很好,并且无论指令元素的维度如何,它们都以正确的维度呈现。

The issue is the rendered directive element itself does take on class properties, which to me makes it look odd in the DOM, as it has an auto height. 问题是渲染的指令元素本身确实采用了类属性,这使得它在DOM中看起来很奇怪,因为它具有auto高度。

Here's a visual of my issue: 这是我的问题的视觉效果:

照片1

Depicted is my directive element, seen here: 描述是我的指示元素,见此处:

照片2

You can see that the months class has been applied to the directive, but its properties have not propagated. 您可以看到months类已应用于该指令,但其属性尚未传播。 So the height and width of the directive go to auto , even though I want 60px . 所以指令的高度和宽度都是auto ,即使我想要60px What is the issue? 有什么问题?

Directive: 指示:

<month ng-model="month" class="months"></month>

Partial template: 部分模板:

app.directive('month', function() {
  return {
    scope:{},
    template:'<div class="months">test</div>',
    link: function (scope, element, attrs) {
      element.addClass('months');
    }
  }
})

As @mosho mentioned in comments, it is probably due to a missing display: block on the <month> element. 正如@mosho在评论中提到的那样,可能是由于缺少display: block <month>元素上的display: block

To expand on this, the initial value for the display style is inline , which would apply unless overriden in a stylesheet somewhere, such as the default browser stylesheet, or the stylesheet(s) you've including on the page. 为了对此进行扩展, display样式的初始值是inline ,除非在某个样式表中覆盖,例如默认的浏览器样式表或您在页面上包含的样式表,否则它将适用。 As <month> is a custom html element, it won't be mentioned in the default browser stylesheet, and as such will be treated to have display: inline , so height and width styles are ignored. 由于<month>是一个自定义html元素,因此在默认浏览器样式表中不会提及它,因此将被视为具有display: inline ,因此将忽略height和width样式。

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

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