![](/img/trans.png)
[英]Angular $scope.$watch is not triggered when i assign $scope to another variable and change the watched value on new variable
[英]Angular $watch not triggered on change in $scope variable
因此,我有一個例程需要與在應用程序初始化時檢索到的用戶數據一起運行。
我的主頁控制器檢查存儲中是否有令牌(已登錄的用戶),然后在$ scope.currentUser已經存在的情況下運行例程,或者在AJAX調用返回該數據時對其進行監視。
但是$ watch回調在初始化后再也不會觸發。
這是Feed Controller代碼
if (sessionStorage.getItem('token'))
{
if ($scope.currentUser) {getItems()}
else {$scope.$watch('currentUser', popFeed()) }
}
function delayForUser()
{
if ($scope.currentUser) {popFeed()}
}
和應用程序控制器代碼
if (sessionStorage.getItem('token') && sessionStorage != 'null')
{
UserSvc.getUser()
.then(function (response)
{
$scope.currentUser = response.data
})
假設您可以從兩個控制器中看到currentUser
(您提到它們是不同的),則需要在$watch
上傳遞一個函數,而不是立即執行它(使用(),這是該函數的結果):
$scope.$watch('currentUser', function(newValue) {
popFeed();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.