Reputation: 1070
So I have a routine that needs to run with user data that is retrieved at the initialization of the app.
My controller for the home page checks to see if there's a token in storage (a logged-in user) and then either runs the routine if $scope.currentUser already exists, or places a watch on it for when the AJAX call returns that data.
But the $watch callback is never triggered after its initialization.
Here's the Feed Controller code
if (sessionStorage.getItem('token'))
{
if ($scope.currentUser) {getItems()}
else {$scope.$watch('currentUser', popFeed()) }
}
function delayForUser()
{
if ($scope.currentUser) {popFeed()}
}
And the Application Controller code
if (sessionStorage.getItem('token') && sessionStorage != 'null')
{
UserSvc.getUser()
.then(function (response)
{
$scope.currentUser = response.data
})
Upvotes: 0
Views: 476
Reputation: 5825
Assuming you can see currentUser
from both controllers (you mentioned they are distinct), you need to pass a function on the $watch
, not execute it immediately (with (), which is the result of that function ):
$scope.$watch('currentUser', function(newValue) {
popFeed();
});
Upvotes: 1