Donny van V
Donny van V

Reputation: 961

Angular watch not updating after focus lose

Currently im checking if a object is changing. After changing im manipulating it and changing some values. After changing i save it to my Service. After all manipulations and savings, i get the value from the service and then append it to $scope.rows. After logging $scope.rows i get the expected results BUT my view isnt updated. Anyone knows how to solve this?

My code:

//Set timeout var only
    var updateDelay = null;
    $scope.$watch('rows', function ( newValue, oldValue )
    {
        //Clear timeout
        clearTimeout(updateDelay);
        //Timeout
        updateDelay = setTimeout(function ()
        {
            //Iterate over all rows to find the updated row
            for ( var i = 0; i < newValue.length; i++ )
            {
                //If start_time, end_time and interval are filled in
                if ( (newValue[i].start_time && oldValue[i].end_time && oldValue[i].interval) &&
                     (
                         (newValue[i].start_time.toString() != oldValue[i].start_time.toString()) ||
                         (newValue[i].end_time.toString() != oldValue[i].end_time.toString()) ||
                         (newValue[i].interval != oldValue[i].interval)
                     ) )
                {
                    //Set the interval to 4 as a test
                    newValue[i].interval = 4;
                    console.log('This row is changed' + i);
                }
            }

            //Push the rows to the Service and then get the new values
            // and bind it to the $scope.rows for the ng-repeat
            $scope.rows = SheetService.setValues(newValue);
        }, 750);
    }, true);

Upvotes: 0

Views: 81

Answers (1)

Walfrat
Walfrat

Reputation: 5353

Replace setTimeout by $timeout and you'll be fine.

This is because a setTimeout will execute outside of angular loop. When you do this you need to call $scope.$apply() as the last instruction. Or you can use $timeout that will do this job for you

Upvotes: 1

Related Questions