2015-06-24 19 views
0

我有一個小問題,我無法在每次輪詢超時時更新視圖中的Poller.msgdata。

所以我想要做的是從服務接收Poller.msgdata和Poller.newdata TRUE或FALSE條件,然後在{{inbox}}內動態輸出。然而,$ scope.inbox自第一次檢索數據是未定義的時候就是空的,這不是問題。它的事實是msgdata和newdata在Poller超時時不會更新。

{{inbox}}每次都應該爲TRUE或FALSE和UPDATE。 如果你需要更多的細節,請讓我知道。

app.controller("taskbarController", ['$scope', 'authData', '$location', 'projectsModal', 'sendMessageModal', 'Poller', 
    function ($scope, authData, $location, projectsModal, sendMessageModal, Poller) { 

    $scope.inbox = Poller.msgdata; 
    $scope.project = Poller.newdata; 
    $scope.projects = Poller.projects; 
    $scope.messages = Poller.messages; 

}]); 

這裏是更新MSGDATA和newdata的輪詢:

app.factory('Poller', Poller); 
Poller.$inject = ['$http', '$timeout']; 

function Poller($http, $timeout) { 

var projectcache = { response: [], calls: 0 }; 
var msgcache = { response: [], calls: 0 }; 
var newdata; 
var msgdata; 

var poller = function() { 
    $timeout(poller, 5000); 
    $http.get('http://localhost/app/controllers/php/getProjects.php') 
    .then(function(r) { 
    if (r.data.projects.length > projectcache.response.length) { 
     newdata = true; 
     projectcolor = 'green'; 
     angular.copy(r.data.projects, projectcache.response); 
    } else { 
     newdata = false; 
     projectcolor = 'green'; 
    }; 
    }); 
    $http.get('http://localhost/app/controllers/php/getMessages.php') 
    .then(function(m) { 
    if (m.data.messages.length > msgcache.response.length) { 
     msgdata = true; 
     msgcolor = 'green'; 
     angular.copy(m.data.messages, msgcache.response); 
    } else { 
     msgdata = false; 
     msgcolor = 'green'; 
    }; 
    }); 
}; 
poller(); 

return { 
    projects: projectcache.response, 
    messages: msgcache.response, 
    newdata: newdata, 
    msgdata: msgdata 
}; 
}; 

最後我顯示部分內部爲:

{{inbox}} 
+0

在控制檯上的任何錯誤? –

+0

@ A.J沒有錯誤我的朋友,因爲沒有什麼語法錯誤。我試圖找出如何在Poller更新其值時更新2 $ scope項目? – Elevant

+0

也許嘗試在你的任務欄控制器中設置'$ timeout(function() {scope。$ apply(); },5000)',以確保範圍正在意識到他正在獲取數據? 否則請嘗試$ interval並每秒記錄您的變量以檢查內容是否更改。 – graphefruit

回答

0

這裏的例子。 。 什麼$ $範圍不適用:What does $scope.$apply() do?

app.controller("taskbarController", ['$scope', 'authData', '$location', 'projectsModal', 'sendMessageModal', 'Poller','$timeout','$interval', 
    function ($scope, authData, $location, projectsModal, sendMessageModal, Poller,$timeout,$interval) { 

    $scope.inbox = Poller.msgdata; 
    $scope.project = Poller.newdata; 
    $scope.projects = Poller.projects; 
    $scope.messages = Poller.messages; 

    $timeout(function() 
    { 
     //Lets see if $scope.$apply does the magic and the data are set 
     $scope.$apply(); 
     console.log($scope.inbox); 
    },5000); 

    //Call every second to see if the variable changes or not. 
    $interval(function() 
    { 

     console.log("check inbox:" + $scope.inbox); 
    },1000); 

}]); 

如果這沒有幫助,嘗試設置你的變量在$範圍$應用功能:https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$apply

app.controller("taskbarController", ['$scope', 'authData', '$location', 'projectsModal', 'sendMessageModal', 'Poller','$timeout','$interval', 
     function ($scope, authData, $location, projectsModal, sendMessageModal, Poller,$timeout,$interval) { 

     $scope.inbox = ""; 
     $scope.project = ""; 
     $scope.projects = ""; 
     $scope.messages = ""; 

     $scope.$apply(function() 
     { 
      $scope.inbox = Poller.msgdata; 
      $scope.project = Poller.newdata; 
      $scope.projects = Poller.projects; 
      $scope.messages = Poller.messages; 
     }); 

    }]); 
相關問題