0

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
    })

1 Answer 1

1

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();
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.