[英]AngularJS: Two-column odd/even layout
Having a problem trying to get a repeating two-column layout in AngularJS. 尝试在AngularJS中获取重复的两列布局时遇到问题。 My dataset is a JSON object of image information.
我的数据集是图像信息的JSON对象。 I want to show a two column layout of images.
我想显示图像的两列布局。 No matter what I tweak, something is wrong in my odd/even logic, but I can't seem to figure it out.
无论我进行什么调整,我的奇/偶逻辑都出了问题,但是我似乎无法弄清楚。 What am I doing wrong?
我究竟做错了什么?
.left { float: left !important; width: 50% !important; } .right { float: right !important; width: 50% !important; } .group:after { content:""; display: table; clear: both; } img { max-width: 100%; height: auto; } @media screen and (max-width: 480px) { .left, .right { float: none; width: auto; } }
<div ng-repeat="issue in issues"> <div ng-if="$even" class="group"> <div class="left" ng-if="$even"> <img src="{{ issue.image }}" ng-src="{{ issue.image }}"> </div> <div class="right" ng-if="$odd"> <img src="{{ issue.image }}" ng-src="{{ issue.image }}"> </div> </div> </div>
The issue with code is you had wrap your logic inside 代码的问题是您将逻辑包装在里面
<div ng-if="$even" class="group">
Div which is restricting to show odd logic div. Div限制显示奇数逻辑div。
instead of having two different div, I'd say use ngClassEven
& ngClassOdd
directive. 而不是使用两个不同的div,我会说使用
ngClassEven
和ngClassOdd
指令。 Also remove the wrapper div which has ng-if="$even"
condition. 还删除具有
ng-if="$even"
条件的wrapper div。
<div ng-repeat="issue in issues">
<div ng-class-even="'left'" ng-class-odd="'right'">
<img ng-src="{{ issue.image }}">
</div>
</div>
I guess you already got your answer, but still here are some alternatives which may prove useful: 我想您已经找到答案了,但是仍然有一些替代方法可能被证明是有用的:
Simply ng-class - it's a little more flexible, so you may find it useful in other cases too. 只是ng级-稍微灵活一点,因此您可能会发现它在其他情况下也很有用。 In this case:
在这种情况下:
<div ng-repeat="issue in issues" ng-class="{left: $even, right: $odd}"> <img ng-src="{{ issue.image }}"> </div>
or 要么
<div ng-repeat="issue in issues" ng-class="$even ? 'left' : 'right'"> <img ng-src="{{ issue.image }}"> </div>
Note that unlike some other properties ng-class
can coexist with class
in harmony so you could also add class="item"
or something similar. 请注意,与其他某些属性不同,
ng-class
可以与class
共存,因此您还可以添加class="item"
或类似的东西。
Since it's a styling issue you may want to try to solve it in css. 由于这是样式问题,因此您可能想尝试在CSS中解决它。 As long as you think IE 6-8 should die you can use the
nth-child
selector: 只要您认为IE 6-8应该消失,就可以使用
nth-child
选择器:
:nth-child(odd) { ... } :nth-child(event) { ... }
Also since both my and Pankaj's answers removed your group
class here is some simpler css which you could use instead: 另外,由于我和Pankaj的答案都删除了您的
group
课程,因此这里有一些更简单的CSS,可以用来代替:
.item {
float: left;
width: 50%;
}
.left {
clear: left;
}
@media screen and (max-width: 480px) {
.item {
float: none;
width: auto;
}
}
Or again if you're not all about IE you could use flexbox (which removes the need for any JS): 或者再次,如果您不完全了解IE,则可以使用flexbox(无需使用任何 JS):
.parent {
display: flex;
flex-wrap: wrap;
}
.item {
flex: 0 0 50%;
}
@media screen and (max-width: 480px) {
.item {
flex-basis: 100%;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.