[英]ng-class on Angular 1.5 component element
I'm converting some directives to components, and was trying to eliminate the extra "wrapping" element since replace: true
doesn't work anymore. 我正在将一些指令转换为组件,并试图消除额外的“包装”元素,因为
replace: true
不再起作用。 However, the old wrapping element had an ng-class on it. 但是,旧的包装元素上有一个ng-class。
<some-directive>
<div ng-class="{classOne: someDirectiveCtrl.isClassOne}">
<p>...</p>
</div>
</some-directive>
In that example, with replace: true
, the div becomes the "wrapping" element. 在该示例中,使用
replace: true
,div成为“包装”元素。
<div ng-class="{classOne: someDirectiveCtrl.isClassOne}">
<p>...</p>
</div>
So... 所以...
Is there a way to access information in a components controller in order to add classes to the component's element? 有没有办法访问组件控制器中的信息,以便将类添加到组件的元素?
ie 即
<some-component ng-class="{classOne: someComponentCtrl.isClassOne}">
<p>...</p>
</some-component>
I'd rather not put functions for the component in the parent controller, and I'd like to have the classes listed in the HTML. 我宁愿不在父控制器中放置组件的函数,我想在HTML中列出这些类。
You need to use ng-controller
instead of ng-class
. 您需要使用
ng-controller
而不是ng-class
。 Assign controller into ng-controller
and then use your class. 将控制器分配到
ng-controller
,然后使用您的班级。
Components always use isolate scope, which is inaccessible from the outside world. 组件始终使用隔离范围,这是外部世界无法访问的。 You'll have to give up the nice declarative approach that ng-class provides and use the available $element in the component controller instead.
你将不得不放弃ng-class提供的漂亮的声明性方法,而是使用组件控制器中的可用$元素。
We gave up and reorganized the html and css so that it wasn't an issue. 我们放弃并重新组织了html和css,这不是问题。 :(
:(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.