Angular ng-style for ::after

不问归期 提交于 2019-12-20 04:12:47

问题


I am designing a dynamic hr (horizonatal) rule.

In my Style sheet

hr.my-hr:after{
   content:'Generic'
}

In my template

<div ng-repeat="name in ['Adam', 'Collin', 'David']">
<hr class="my-hr" ng-style="{'content':name}">

But how do i dynamically change the content in the template when using ng-repeat ????


回答1:


All ng-style does is add inline styles. However you want to add a psuedo element ::after. According to the MDN:

You can use only one pseudo-element in a selector. It must appear after the simple selectors in the statement.

So inferred from that you can't use them inline, which sadly means ng-style can't do what your after.

However if your ::after is defined in a stylesheet you can use ng-class to dynamically add that style.

So

<hr ng-class="{'my-hr': <some condition to evaluate>}" />

Most cases that will suffice. However it looks like to want to dynamically set the content of ::after. So for that i can only imagine two options.

If you just want to simply add the string value use databinding

<hr />
{{name}}

However if you want extra styling on that string create a small directive as a re-usable widget may be the better option.



来源:https://stackoverflow.com/questions/35356952/angular-ng-style-for-after

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!