从n个元素的数组中一次显示5个元素,并在angularjs中重复bigbegin
Displaying 5 items at a time from an array of n items and again repeat from biginning in angularjs
我是angularJs的新手,我有一个情况,我必须从多个项目的数组中显示5个项目。即首先显示1-5个项目,然后过2-3秒在顶部插入第6个项目,并从底部删除第一个项目。数组显示为上第5,下第4,依此类推。所以每次用下一个项目替换顶部项目,并删除底部项目。当显示最后一个项目时,也从第一个项目返回。当最后一项显示时,下一项应该是第一项。
$scope.activityFeedArray = function() {
$scope.activityFeed[0] = $scope.actFeedArr[$scope.idx1];$scope.idx1=$scope.idx1+1;if($scope.idx1>=20){$scope.idx1=20-$scope.idx1};
$scope.activityFeed[1] = $scope.actFeedArr[$scope.idx2];$scope.idx2=$scope.idx2+1;if($scope.idx2>=20){$scope.idx2=20-$scope.idx2};
$scope.activityFeed[2] = $scope.actFeedArr[$scope.idx3];$scope.idx3=$scope.idx3+1;if($scope.idx3>=20){$scope.idx3=20-$scope.idx3};
$scope.activityFeed[3] = $scope.actFeedArr[$scope.idx4];$scope.idx4=$scope.idx4+1;if($scope.idx4>=20){$scope.idx4=20-$scope.idx4};
$scope.activityFeed[4] = $scope.actFeedArr[$scope.idx5];$scope.idx5=$scope.idx5+1;if($scope.idx5>=20){$scope.idx5=20-$scope.idx5};
};
$interval(function() {$scope.activityFeedArray2();} , 1000);
按如下方式使用angularjs的$Interval函数:
$interval(function() {activityFeedArray2();} , 1000);
function activityFeedArray2() {
$scope.activityFeed.shift();
$scope.activityFeed.push(actFeedArr[counter]);
counter=counter+1;
if(counter>=actFeedArr.length) {
counter = counter-actFeedArr.length;
};
};
下面是运行良好的代码:
app.controller("activityFeedCntrl",function($scope,$interval) {
$scope.activityFeed=[{id:1,msg:'a clicked'},{id:2,msg:'b clicked'},{id:3,msg:'c clicked'},{id:4,msg:'d clicked'},
{id:5,msg:'e clicked'}];
var counter = $scope.activityFeed.length;
var actFeedArr = [{id:1,msg:'a clicked'},{id:2,msg:'b clicked'},{id:3,msg:'c clicked'},{id:4,msg:'d clicked'},
{id:5,msg:'e clicked'},{id:6,msg:'f clicked'},{id:7,msg:'g clicked'},{id:8,msg:'h clicked'},{id:9,msg:'i clicked'},
{id:10,msg:'j clicked'},{id:11,msg:'k clicked'},{id:12,msg:'l clicked'},{id:13,msg:'m clicked'},{id:14,msg:'n clicked'},
{id:15,msg:'o clicked'},{id:16,msg:'p clicked'},{id:17,msg:'q clicked'},{id:18,msg:'r clicked'},{id:19,msg:'s clicked'},
{id:20,msg:'w clicked'}];
$interval(function() {activityFeedArray2();} , 1000);
function activityFeedArray2() {
$scope.activityFeed.shift();
$scope.activityFeed.push(actFeedArr[counter]);
counter=counter+1;
if(counter>=actFeedArr.length) {
counter = counter-actFeedArr.length;
};
};
});
在html页面。
<!DOCTYPE html>
<html ng-app="activityFeedApp">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<head>
<title>Activity Feed</title>
</head>
<body>
<div ng-controller="activityFeedCntrl">
<p>Activity Feed</p>
<table>
<tr ng-repeat="feed in activityFeed track by $index">
<td ng-bind="feed.id"></td>
<td ng-bind="feed.msg"></td>
</tr>
</table>
</div>
<script src="activityFeedApp.js" ></script>
<script src="activityFeedCntrl.js" ></script>
</body>
</html>
相关文章:
- 使用clickToggle并在单击另一个元素时关闭元素
- 遍历类元素数组,并在jquery中选择同级元素
- 如何覆盖锚点元素's href目标,并在我点击转到目标javascript时删除其他错误
- 如何按类选择元素并在 D3 中检索其 ID
- 隐藏元素并在浏览器窗口宽度小于时显示其他
- nextAll() 可以跳过任何指定的父元素并在父元素中查找唯一的子元素
- 如何匹配所有元素并在页面中获取它们的内容
- 如何在g元素中选择特定元素并在D3.js中更改其状态
- jQuery-如何动画滚动到元素并在顶部添加100px
- 如何链接到不同页面上的特定元素并在其上执行jQuery脚本
- 迭代所有“选择”元素并在Selenium中获取它们的所有值
- 如何通过OnClick函数循环遍历标记元素并在显示器上获得相应的内容
- 如何从json数组中选择一个元素并在html上显示
- 在多维数组中查找元素并在其后面添加一些东西
- 如何滚动到元素并在到达视口顶部时停止
- 如何遍历具有特定类的所有元素并在其中找到输入类型?
- AngularJS指令,用于存储DOM元素并在表单提交时重定向
- 调用一个html元素并在JavaScript中对其进行操作
- 如何使用jquery替换变量中的元素并在另一个变量中获取结果
- 通过 Id 获取元素并在 JavaScript 中设置值