1

我使用的可以在這裏找到了ngStorage模塊:https://github.com/gsklee/ngStorage

我設置一個lastSeenId在localStorage的爲實時更新社交飼料,因爲這是一個混合的應用程序(Laravel/AngularJS)我無法將其存儲在$scope中。

StatusesController得到一個數組或數據(或什麼,如果沒有查看)以往X秒,我得到的數組的最後一個項目,並設置我的$localStorage VAR是這樣的: $localStorage.lastStatusSeen = lastId;

我的導航控制器在這裏我爲用戶設置了一些新的計數器,以查看他們是否錯過了任何東西,我得到的lastSeenId是這樣的: $scope.$storage.lastStatusSeen它被髮送到我的API返回一些未看到的狀態帖子。

這一切都有效,用戶在狀態頁面上,如果新數據,然後將本地存儲var設置爲數據數組中的最後一個ID。

問題是我的NavigationController未找到此更改並始終通過用戶第一次訪問該頁面時傳遞的lastSeenId

有沒有辦法在本地存儲中查看該密鑰以進行更改?

編輯:按照要求,更新的代碼

app.controllers.navigationController = ['$scope', 'pollingService', '$localStorage', function($scope, pollingService, $localStorage) { 

    $scope.lastStatusSeen = $localStorage.lastStatusSeen; 

    $scope.$watch(function() { 
     return $localStorage.lastStatusSeen; 
    }, function (newVal, oldVal) { 
     if (!newVal) { 
      return; 
     } 

     if (newVal !== oldVal) { 
      // Assign to a local scoped var, or anything, here... 
      $scope.lastStatusSeen = newVal; 
      // I suggest call a method to do your logic outside this 
      //$scope.onIdChange(newVal, oldVal); // If you need oldVal 
     } 
    }); 

    pollingService.startPolling('emailCount', 'api/email/new-count', 5000, function(response) { 
     $scope.emailCount = response.data; 
    }); 

    pollingService.startPolling('statusCount', 'api/social/new-count?lastid=' + $scope.lastStatusSeen, 5000, function(response) { 
     $scope.socialCount = response.data; 
    }); 

}]; 

回答

1

可以使用的angular$scope$rootScope)任何範圍的實體的$watch方法看它:

$scope.$watch(function() { 
    return $localStorage.lastStatusSeen; 
}, function (newVal, oldVal) { 
    if (!newVal) { 
     return; 
    } 

    if (newVal !== oldVal) { 
     // Assign to a local scoped var, or anything, here... 
     // I suggest call a method to do your logic outside this 

     $scope.onIdChange(newVal, oldVal); // If you need oldVal 
    } 
}); 

小心性能,因爲這幾乎每個$digest週期。另一個需要注意的是,你可以在此守望者到一個變量關聯,這樣你就可以取消任何時候你需要:

var watchIdStored = $scope.$watch(function() { 
    /* ... */ 

當你想刪除它,調用var當成一個函數:

watchIdStored(); // Removes the $watcher 

編輯1:

問題在pollingService.startPolling('statusCount', 'api/social/new-count?lastid=' + $scope.lastStatusSeen。它建立了一個不再被改變的字符串。您必須在發生變化時通知此更改,並向投票提供更新。

如果你把一個控制檯放在watcher裏面,更新var,你會看到它正在更新。

編輯2:

當你有一個stopPolling()方法,你可以創建一個構建/控制器上破壞功能。

function _setupPolling (name, url, pollingTime, callback) { 
    pollingService.stopPolling(name); 

    pollingService.startPolling.apply(pollingService, arguments); 
} 

現在,你把它的每上$localStorage.lastStatusSeen發生變化:

$scope.$watch(function() { 
    return $localStorage.lastStatusSeen; 
}, function (newVal, oldVal) { 
    if (!newVal) { 
     return; 
    } 

    if (newVal !== oldVal) { 
     console.log(newVal); 

     // Assign to a local scoped var, or anything, here... 
     $scope.lastStatusSeen = newVal; 
     // I suggest call a method to do your logic outside this 

     _setupPolling('statusCount', 'api/social/new-count?lastid=' + $scope.lastStatusSeen, 5000, function(response) { 
      $scope.socialCount = response.data; 
     }); 
    } 
}); 
+0

這似乎並不工作,我做如上代碼提示,我將其分配給一個範圍var當我的AJAX調用仍顯示ID 23,當本地存儲顯示24? – ChrisBratherton

+0

問題已更新,我將其放置在我的導航控制器中? – ChrisBratherton

+0

但是,如果我在監視器中放入pollingService調用,只會在ID更改後才調用該調用?該功能每5秒左右輪詢一次服務器? – ChrisBratherton