2014-10-03 46 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