简体   繁体   English

对每种样式应用独特的样式 <li> 在Angular的ng-repeat列表中

[英]Apply unique styles to each <li> in an ng-repeat list in Angular

I am very new to angular and I am having some difficulty wrapping my head around how to handle certain things "the Angular way". 我对angular非常陌生,在解决“ Angular方式”中的某些问题时遇到了一些困难。 Please forgive my ignorance in this post. 请原谅我对此信息的无知。

I currently have 6 list items that are created by hitting a data source and using ng-repeat . 我目前有6个列表项,这些列表项是通过点击数据源并使用ng-repeat

<ul>
  <li ng-repeat="vote in votes" ng-click="vote.Score = vote.Score + 1">
    {{ vote.Id | covertToName }} has a score of {{ vote.Score }}
  </li>
</ul>

Each of these list items has a "progress bar" below it which is simply a absolutely positioned :after psuedo element. 每个列表项下面都有一个“进度条”,它只是:after psuedo元素:after绝对定位的:after My aim is to increase the width of the progress bar's :after psuedo element when you click on the list item to give you a visual display of the number of votes each element has. 我的目的是在单击列表项时增加进度条的:after psuedo元素的宽度,以便直观地显示每个元素的投票数。

I need a way to apply a custom style ( width: ) to each of the list items created by the ng-repeat when a user clicks on a list item. 我需要一种在用户单击列表项时将自定义样式( width:应用于ng-repeat创建的每个列表项的方法。 For example, if a user clicks on John Doe and his Score is currently at 50, I need his progress bar to assume a width of 51px as well as apply the score pixel width to all other list items. 例如,如果用户单击John Doe,并且他的“分数”当前为50,则我需要他的进度条假定其宽度为51px,并将分数像素的宽度应用于所有其他列表项。

Any direction is greatly appreciated. 任何方向都将不胜感激。

Thank You! 谢谢!

Edit: I am using SCSS and I have no control over the JSON data source. 编辑:我正在使用SCSS,并且无法控制JSON数据源。

If you're using a CSS preprocessor like SASS you can easily achieve this by using ng-class="progress" . 如果您使用的是CSS预处理器(如SASS),则可以使用ng-class="progress"轻松实现。 progress would hold a string like progress-51 . progress将包含一个字符串,例如progress-51

The SASS-code SASS代码

$class-slug: progress !default

@for $i from 0 through 100
    .#{$class-slug}-#{$i}:after
        width: 0px + $i

would emit 会发出

.progress-0:after {
    width: 0px;
}
.progress-1:after {
    width: 1px;
}
...
.progress-100:after {
    width: 100px;
}

You can try as below 您可以尝试如下

li:first-child:after { /*styles*/ }
li:second-child:after { /*styles*/ }
li:third-child:after { /*styles*/ }
li:fourth-child:after { /*styles*/ }
li:fifth-child:after { /*styles*/ }
li:sixth-child:after { /*styles*/ }

If you need exact pixels based of values I dont think classes is really useful since you don't know what element need what style. 如果您需要基于值的精确像素,我认为类不是真正有用的,因为您不知道哪个元素需要哪种样式。 Perhaps you can use inline styles for this even though it is not best practise. 即使不是最佳实践,也许您也可以使用内联样式。

Example: 例:

<ul>
  <li ng-repeat="vote in votes" ng-click="vote.Score = vote.Score + 1">
    <span style="width:{{vote.score}}px;>{{ vote.Id | covertToName }} has a score of {{ vote.Score }}</span>
  </li>
</ul>

mm.. u can create attrib in object votes, like this: mm ..您可以在对象投票中创建attrib,如下所示:

var votes = [{Id:0,Score:0,width:"first"},{Id:1,Score:0,width:"second"},{Id:2,Score:0,width:"third"}];

And then u can use ng-class to do a unique style each li: 然后,您可以使用ng-class为每个li制作独特的样式:

<ul>
  <li ng-repeat="vote in votes" ng-class="vote.width" vote.ng-click="vote.Score = vote.Score + 1">
    {{ vote.Id | covertToName }} has a score of {{ vote.Score }}
  </li>
</ul>

and need define this styles: 并需要定义以下样式:

.second {
  width: 50%;
  background:red;
}
.first {
  width: 20%;
  background: blue;
}
.third {
  width: 30%;
  background: orange;
}

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

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