2013-10-08 53 views
1

我正在開發角JS的新聞Feed頁面。每當用戶更改應用程序時,都應該在新聞頁面上進行更新。我正在顯示新聞Feed頁面中的更改。但問題是,當我刷新頁面時,它顯示了變化。我必須在不刷新頁面的情況下顯示更改。我從http url加載數據。Angular JS News Feed不會自動更新

到目前爲止,我試圖 的html代碼:

<div> ng-controller="TopListCtrl" 
    <div class="cb-feed-wrapper bradius5" ng-repeat="post in posts"> 

<h3>{{ post.userId}}</h3> 
<p> {{post.change}} </p><p> Created On : {{ post.createdOn }} | Updated On : {{ post.updatedOn }} </p><p Time: {{ post.dateTime }}</p> 
</div></div> 

控制器代碼:

function TopListCtrl($scope, $http, $timeout) { 
$scope.refreshInterval = 1; 
$http({ 
     url: '/zzzz/v1/zzzz/all?callback=JSON_CALLBACK', 
     method: "GET" 

    }).success(function (data) { 
      $scope.posts = data; 

     }).error(function (data) { 
     }); 
$timeout(function() { 
    $scope.refreshInterval * 5; 
}); 
+0

我猜你想試探更新?在初始頁面加載之後,你永遠不會打電話(你的'$ timeout'簡單地更新了一個不用於調度另一個'$ http'調用的refreshInterval var。你的模板和posts中有'post'你假設你跳過了一個'ng-repeat'容器嗎? –

+0

你能提供任何示例代碼嗎? –

+0

@kevin stone:任何引用鏈接或示例程序 –

回答

0

您需要實現某種輪詢機制,使重複http電話,在設定的時間間隔,如果你想再次刷新數據(你沒有這樣做)。看到這裏http://plnkr.co/edit/iMmhXTYweN4IrRrrpvMq?p=preview

+0

我試過這個示例,但它碰到了服務器但無法設置響應返回 –

+0

這是給你一個想法,你究竟做了什麼,你說什麼意思,當你說無法設置響應回來 – Chandermani

+0

後點擊輪詢函數數據不能設置範圍$範圍。 data = Poller.data; –

5

一個例子撥弄你的控制器看起來應該是這樣:

function TopListCtrl($scope, $http, $interval) { 
    $scope.refreshInterval = 5; // For every 5 sec 

    // Create a function as we will reuse this code 
    function refresh() { 
    $http({ 
     url: '/lexaserver/v1/audit/all?callback=JSON_CALLBACK', 
     method: "GET" 

    }).success(function (data) { 
     $scope.posts = data; 

    }).error(function (data) { 
     console.log('Error'); 
    }); 
    } 

    refresh(); // We call the function on initialization to load the feed. 

    // $interval runs the given function every X millisec (2nd arg) 
    $interval(function() { 
    refresh(); 
    }, $scope.refreshInterval * 1000); // the refresh interval must be in millisec 
} 

但我不知道你想實現與$scope.refreshInterval什麼。

+0

我試過這段代碼,但是它說$間隔沒有定義, –

+0

我正在顯示頁面中的數據,但頁面應該自動更新val的時間 –

+0

是的抱歉,我忘了在第一行中將'$ timeout'依賴項更改爲'$ interval'。 – maxdec