我正在尝试在我的应用中设置间隔,并有类似的东西
HTML
<div class="text">
{{currentItem.name}}
</div>
<ul>
<li ng-repeat="item in items" ng-click="pickItem($index)">{{item.type}}</li>
</ul>
控制器
$scope.index = 0;
$scope.currentItem = $scope.items[$scope.index]; // set the first item
//The interval start 3 second after the app is loaded
$scope.startInt = $interval(function() {
$scope.currentItem = $scope.items[$scope.index];
$scope.index++;
if(scope.index === $scope.items.length) {
$scope.index=0;
}
}, 3000)
以上代码将开始间隔&#39; AFTER&#39;页面加载3秒。我希望在页面首次加载时立即启动间隔。反正有没有这样做?非常感谢!
答案 0 :(得分:1)
一个简单的解决方案:
var intervalWork = function () {
$scope.currentItem = $scope.items[$scope.index];
$scope.index++;
if(scope.index === $scope.items.length) {
$scope.index=0;
}
};
intervalWork();
//The interval start 3 second after the app is loaded
$scope.startInt = $interval(intervalWork, 3000)
答案 1 :(得分:1)
将您的匿名函数转换为命名函数。然后在使用区间
之前调用该函数function IntervalFunction() {
$scope.currentItem = $scope.items[$scope.index];
$scope.index++;
if(scope.index === $scope.items.length) {
$scope.index=0;
}
}
IntervalFunctin();
$scope.startInt = $interval(IntervalFunction, 3000);
答案 2 :(得分:1)
function notificationsCtrl($scope, $interval) {
var fun = function(){
$scope.currentItem = $scope.items[$scope.index];
$scope.index++;
if(scope.index === $scope.items.length) {
$scope.index=0;
};
fun();
$interval(fun, 3000);
};