简体   繁体   English

可以将指令仅用作AngularJs中的UI组件吗?

[英]OK to use a directive only as an UI component in AngularJs?

I am having an UI component which is reusable in multiple places. 我有一个可在多个地方重用的UI组件。 Therefore I have to duplicate the same css sections over several css files. 因此,我必须在几个CSS文件中复制相同的CSS部分。 Is it ok to create a directive for this scenario ? 是否可以为此场景创建指令? The directive will not process any js specific operations, it is there only for handling the UI component and avoid the duplication. 该指令将不处理任何特定于js的操作,它仅用于处理UI组件并避免重复。

NOTE: You've specified “Angular” in the question title, but angularjs as a tag. 注意:您已经在问题标题中指定了“ Angular”,但是将angularjs作为标记。 This answer assumes you're coding for Angular, not AngularJS. 此答案假定您正在为Angular而不是AngularJS进行编码。 Consider editing the tag or the question title to remove this ambiguity. 考虑编辑标签或问题标题以消除这种歧义。 ( What's the difference between AngularJS and Angular? ) AngularJS和Angular有什么区别?

Angular can bundle style information with components. Angular可以将样式信息与组件捆绑在一起。 You can specify the CSS: 您可以指定CSS:

  1. inline using the styles attribute of the @Component decorator, or 使用@Component装饰器的styles属性内联,或
  2. in an external file using the styleUrls attribute of the @Component decorator. 在外部文件中使用@Component装饰器的styleUrls属性。

See the Component Styles section of the Angular docs for examples & more info. 有关示例和更多信息,请参见Angular文档的Component Styles部分。

While you could technically write an attribute directive to style your component, why you would choose to do this instead of using the styling functionality provided by components is unclear. 从技术上讲,您可以编写属性指令来设置组件的样式,但不清楚为什么选择这样做而不是使用组件提供的样式功能。 Expanding your question with an example or two of what you're trying to achieve may bring you a better answer. 用一个或两个示例来扩展您要解决的问题,可能会给您带来更好的答案。

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

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