如何遍历数组并将值放在 AngularJS 中的唯一标签中
how to loop through an array and put values in unique labels in AngularJS
Issue
我正在尝试向用户显示他们获得的接下来的 4 个事件。到目前为止,我已经在数组上使用splice
来选择前 4 个objects
。所以现在我有了这些对象,我需要能够遍历数组并显示正确的数据。
所有标签都是唯一的,并且具有不同的名称。
我不知道我这样做的方式是否正确。这对我来说是一个学习项目。
法典
我的 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>
如您所见,第一行显示下一个事件是 10 天 - 在它下面你可以看到它是一个假期。如果您继续浏览,则此信息有 4 个不同的标签。
控制器代码:
function GetNext4UserEvents() {
var top4 = _allUserEvents.splice(0, 4);
}
我并没有真正添加太多内容,因为我只是想不出最好的方法。如您所见,尽管我得到了前 4 个对象。
对象如下所示:
0: Object
color: "#cc0000"
end: "2016-02-20"
id: 6
start: "2016-02-10"
title: "Test2"
我希望
我没有完全误解你的问题,但这里有一个输出你的事件的尝试。也许这不是你所追求的,但希望它可以作为一个起点。我创建了一个jsFiddle,您可以在其中弄乱代码。
.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:
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"
} ];
});
相关文章:
- 当使用select作为标签时,如何使用ng选项禁用angularjs中的选项
- AngularJS:类的优选顺序=“”"并且ng点击=“"在标签中
- Cordova AngularJS Ionic标签的内容在第一次加载时重叠
- Html不是'我没认出Angularjs吐出来的标签
- 在AngularJS中过滤-html标签中的文本
- 如何从表单标签外部的按钮手动触发 AngularJS 验证
- 如何知道点击了哪个“a”标签?AngularJs Way.
- 标签在 AngularJS 中不起作用
- Angularjs:如何捕获
标签中的文本并在我的控制器中使用它 - 调用AngularJS内部<脚本>标签
- AngularJS-配置多个基本标签
- 使用angularjs交换图像标签中的ng-src属性
- 数据绑定在带有输入标签的 AngularJS 中不起作用
- 将信息框作为HTML标签属性(AngularJS)添加到Google Maps Marker
- javascript AngularJS在包装标签时自动更改字体大小
- 如何遍历数组并将值放在 AngularJS 中的唯一标签中
- 如何在angularjs中将html标签转换为文本
- 如何显示下拉 Angularjs-ui-select onclick on 超链接标签
- 如何在 angularjs 中使用 li 和 ul 标签的引导网格
- AngularJS标签没有'不能在innerHTML中工作