[英]how to loop through an array and put values in unique labels in AngularJS
I am trying to display to the user the next 4 events they have got. 我试图向用户显示他们接下来的4个事件。 So far I have used splice
on the array to select the first 4 objects
. 到目前为止,我已经在数组上使用splice
来选择前4个objects
。 So now I have these objects I need to be able to loop through the array and display the correct data. 所以现在我有了这些对象,我需要能够遍历数组并显示正确的数据。
All the labels are unique and will have different names. 所有标签都是唯一的,并且名称不同。
I don't know if the way i am doing this is the correct way. 我不知道我这样做的方式是否正确。 This is a learning project for me. 这对我来说是一个学习项目。
My ASP.NET code for the 4 events looks like this: 我的4个事件的ASP.NET代码如下所示:
<div class="row" style="padding-top: 10px; padding-bottom: 10px; border: 1px solid #ddd; border-radius: 2px; margin-top: 25px; border-left: 4px solid #21cd25; font-size: 12px;">
<div class="col-lg-12">
<span class="glyphicon glyphicon-ok" style="color: #21cd25; padding-right: 10px;"></span>
<span style="font-weight: 500;">10 Days - </span>
<span>Holiday</span>
</div>
</div>
<div class="row" style="padding-top: 10px; padding-bottom: 10px; border: 1px solid #ddd; border-radius: 2px; margin-top: 5px; border-left: 4px solid #f6bb42; font-size: 12px;">
<div class="col-lg-12">
<span class="glyphicon glyphicon-question-sign" style="color: #f6bb42; padding-right: 10px;"></span>
<span style="font-weight: 500;">3 Weeks - </span>
<span>Paternity</span>
</div>
</div>
<div class="row" style="padding-top: 10px; padding-bottom: 10px; border: 1px solid #ddd; border-radius: 2px; margin-top: 5px; border-left: 4px solid #cd2121; font-size: 12px;">
<div class="col-lg-12">
<span class="glyphicon glyphicon-remove" style="color: #cd2121; padding-right: 10px;"></span>
<span style="font-weight: 500;">1 Week - </span>
<span>Holiday</span>
</div>
</div>
<div class="row" style="padding-top: 10px; padding-bottom: 10px; border: 1px solid #ddd; border-radius: 2px; margin-top: 5px; border-left: 4px solid #21cd25; font-size: 12px;">
<div class="col-lg-12">
<span class="glyphicon glyphicon-ok" style="color: #21cd25; padding-right: 10px;"></span>
<span style="font-weight: 500;">1 Day - </span>
<span>Bank Holiday</span>
</div>
</div>
As you can see the first row says the next event is 10 Days - away and below that you can see it is a Holiday . 正如你所看到的那样,第一行表示下一个事件是10天 -远离和低于你可以看到它是一个假期 。 If you keep looking through there are 4 different labels for this information. 如果您继续浏览,此信息有4种不同的标签。
Controller code: 控制器代码:
function GetNext4UserEvents() {
var top4 = _allUserEvents.splice(0, 4);
}
I have not really added much to this as i just can't think of the best way to do it. 我没有真正添加太多,因为我只是想不出最好的方法。 As you can see though i am getting the top 4 objects. 如您所见,尽管我获得了前4个对象。
The object looks like this: 该对象如下所示:
0: Object
color: "#cc0000"
end: "2016-02-20"
id: 6
start: "2016-02-10"
title: "Test2"
I hope that I haven't completely misunderstood your question, but here's a stab at outputting your events. 我希望我没有完全误解你的问题,但这是一个输出你的事件的刺刀。 Maybe it's not exactly what you were after, but hopefully it can serve as a starting point. 也许这与您追求的不完全相同,但希望它可以作为起点。 I've created a jsFiddle where you can mess around with the code. 我创建了一个jsFiddle ,您可以在其中弄乱代码。
HTML: HTML:
<div ng-app="app">
<div ng-controller="ctrl">
<div class="row" style="padding-top: 10px; padding-bottom: 10px; border: 1px solid #ddd; border-radius: 2px; margin-top: 25px; border-left: 4px solid #21cd25; font-size: 12px;" ng-repeat="event in sampleEvents">
<div class="col-lg-12">
<span class="glyphicon {{event.color | findIcon}}" ng-style="{'color': event.color, 'padding-right': '10px'}"></span>
<span style="font-weight: 500;">{{event.start | daysOut}} - </span>
<span>{{event.title}}</span>
</div>
</div>
</div>
</div>
JS: JS:
angular.module("app", ["angularMoment"])
.filter("daysOut", function(moment) {
return function(input) {
return moment.duration(moment(input) - moment()).humanize();
};
})
.filter("findIcon", function() {
return function(input) {
switch (input) {
case "#cc0000":
return "glyphicon-remove";
break;
case "#008000":
return "glyphicon-ok";
break;
case "#ffa500":
return "glyphicon-question-sign";
break;
}
return "glyphicon-ok";
}
})
.controller("ctrl", function($scope) {
$scope.sampleEvents = [{
color: "#cc0000",
end: "2016-04-01",
id: 1,
start: "2016-03-19",
title: "Event 1"
}, {
color: "#008000",
end: "2016-04-03",
id: 2,
start: "2016-03-22",
title: "Event 2"
}, {
color: "#ffa500",
end: "2016-04-15",
id: 3,
start: "2016-04-02",
title: "Event 3"
}, {
color: "#008000",
end: "2016-04-14",
id: 4,
start: "2016-03-13",
title: "Event 4"
} ];
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.