2014-10-03 44 views
0

我在我的控制器中使用了$timeout,但它不工作!

app.controller("Friendsrequests",['$http','$log','$location','$timeout','$scope', function($http,$log,$location,$timeout,$scope){ 
//getting base url of application 
this.baseUrl = function() { 
     var base_url = window.location.origin; 

     var pathArray = window.location.pathname; 
     return base_url; 
    // return base_url+pathArray; 
       }; 
// assigning to a variablebase_url('login/f_request'); 
var ThroughUrl = this.baseUrl()+'/keon/login/f_request'; 


//declare a variable 
var ata = this; 
ata.notifications = [ ] ; 
ata.counts=' '; 
//sending ajax request 
function getNotifications() 
{ 
$http({method: 'POST', url: ThroughUrl,}) 
.success(function(data) { 
    // this callback will be called asynchronously 
    // when the response is available 

//assign data to the variable 
ata.notifications=data; 
ata.counts =data.length; 

    }). 
    error(function(data, status, headers, config) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status. 
    }); 

} 

$timeout(function() { 
    getNotifications(); 
}, 1000); 

}]); 
  • 什么问题。
+0

什么不工作呢?它是否因错误而失败,是否被调用等? – sma 2014-10-03 14:02:57

+0

控制台是空的没有错误控制器成功传递数据查看,但它不重复的功能(ajax调用) – user3636439 2014-10-03 14:05:35

回答

9

修订

只需更换

$timeout(function() { 
    getNotifications(); 
}, 1000); 

$interval(function() { 
    getNotifications(); 
},1000); 

检查Angular's doc

+0

是现在正在工作,但我在控制台中有这个错误错误:[$ rootScope:inprog] http://错误.angularjs.org/1.3.0-rc.4/$ rootScope/inprog?p0 =%24digest – user3636439 2014-10-03 14:12:25

+1

请注意,有一个[$ interval](https://docs.angularjs.org/api/ng/service/$间隔)服务。我猜的答案解释了为什么在2013年8月没有存在美元间隔现在只是过时:) – 2014-10-03 14:12:25

+0

我可以不必注入$应用到控制器? – user3636439 2014-10-03 14:13:04