Update scope value when service data is changed

Alex Grs

I have the following service in my app:

uaInProgressApp.factory('uaProgressService', 
    function(uaApiInterface, $timeout, $rootScope){
        var factory = {};
        factory.taskResource = uaApiInterface.taskResource()
        factory.taskList = [];
        factory.cron = undefined;
        factory.updateTaskList = function() {
            factory.taskResource.query(function(data){
                factory.taskList = data;
                $rootScope.$digest
                console.log(factory.taskList);
            });
            factory.cron = $timeout(factory.updateTaskList, 5000);
        }

        factory.startCron = function () {
            factory.cron = $timeout(factory.updateTaskList, 5000);
        }

        factory.stopCron = function (){
            $timeout.cancel(factory.cron);
        }
        return factory;
});

Then I use it in a controller like this:

uaInProgressApp.controller('ua.InProgressController',
    function ($scope, $rootScope, $routeParams, uaContext, uaProgressService) {
        uaContext.getSession().then(function(){
            uaContext.appName.set('Testing house');
            uaContext.subAppName.set('In progress');
            uaProgressService.startCron();

            $scope.taskList = uaProgressService.taskList;
        });
    }
);

So basically my service update factory.taskList every 5 seconds and I linked this factory.taskList to $scope.taskList. I then tried different methods like $apply, $digest but changes on factory.taskList are not reflected in my controller and view $scope.taskList.

It remains empty in my template. Do you know how I can propagate these changes ?

Piacenti

While using $watch may solve the problem, it is not the most efficient solution. You might want to change the way you are storing the data in the service.

The problem is that you are replacing the memory location that your taskList is associated to every time you assign it a new value while the scope is stuck pointing to the old location. You can see this happening in this plunk.

Take a heap snapshots with Chrome when you first load the plunk and, after you click the button, you will see that the memory location the scope points to is never updated while the list points to a different memory location.

You can easily fix this by having your service hold an object that contains the variable that may change (something like data:{task:[], x:[], z:[]}). In this case "data" should never be changed but any of its members may be changed whenever you need to. You then pass this data variable to the scope and, as long as you don't override it by trying to assign "data" to something else, whenever a field inside data changes the scope will know about it and will update correctly.

This plunk shows the same example running using the fix suggested above. No need to use any watchers in this situation and if it ever happens that something is not updated on the view you know that all you need to do is run a scope $apply to update the view.

This way you eliminate the need for watchers that frequently compare variables for changes and the ugly setup involved in cases when you need to watch many variables. The only issue with this approach is that on your view (html) you will have "data." prefixing everything where you used to just have the variable name.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update scope value when service data changes

From Dev

Data change inside Service does not update the Scope

From Dev

how to update cache only when data is changed?

From Dev

Update variable when data in databases has changed

From Dev

Scope value not updating when service variable changes

From Dev

How to update data on server via AJAX when ng-model input value is changed

From Dev

Update vue.js data automatically when binded input field's value is changed by JavaScript

From Dev

Update vue.js data automatically when binded input field's value is changed by JavaScript

From Dev

How do I automatically update the value of a variable when a data is changed in ionic 3?

From Dev

AngularJS directive scope not updating when value changed outside of AngularJS

From Dev

Angular - save scope value to variable, but not update it when scope updates

From Dev

Service Worker: how to update the cache when files changed on the server?

From Dev

Check if value has changed when moving data

From Dev

$scope is not refreshing after value changed

From Dev

angular scoped variable in controller not updating when value changed in service

From Dev

Auto-refreshing a service value on several controllers when changed

From Dev

MySQL no affected rows upon UPDATE when value not changed

From Dev

Update TextBox text when a ComboBox's value is changed

From Dev

WPF - Value inside datagrid fails to update converter when changed externally

From Dev

How to update AngularJS view when the value has not changed?

From Dev

WPF usercontrol property binding to Inotifypropertychanged, update only when value changed

From Dev

AngularJS : Watch scope value of a injected service changed in one directive from another directive

From Dev

MySQL update datetime column data only when it has changed for that record

From Dev

How to update element when data-* attribute changed

From Dev

Update React when Data in Object in Store has changed

From Dev

How to update search results when scope button changed. Swift UISearchController

From Dev

How to update md-select options when scope value changes?

From Dev

How to update md-select options when scope value changes?

From Dev

How to update UI using angularJs when scope value changes?

Related Related

  1. 1

    Update scope value when service data changes

  2. 2

    Data change inside Service does not update the Scope

  3. 3

    how to update cache only when data is changed?

  4. 4

    Update variable when data in databases has changed

  5. 5

    Scope value not updating when service variable changes

  6. 6

    How to update data on server via AJAX when ng-model input value is changed

  7. 7

    Update vue.js data automatically when binded input field's value is changed by JavaScript

  8. 8

    Update vue.js data automatically when binded input field's value is changed by JavaScript

  9. 9

    How do I automatically update the value of a variable when a data is changed in ionic 3?

  10. 10

    AngularJS directive scope not updating when value changed outside of AngularJS

  11. 11

    Angular - save scope value to variable, but not update it when scope updates

  12. 12

    Service Worker: how to update the cache when files changed on the server?

  13. 13

    Check if value has changed when moving data

  14. 14

    $scope is not refreshing after value changed

  15. 15

    angular scoped variable in controller not updating when value changed in service

  16. 16

    Auto-refreshing a service value on several controllers when changed

  17. 17

    MySQL no affected rows upon UPDATE when value not changed

  18. 18

    Update TextBox text when a ComboBox's value is changed

  19. 19

    WPF - Value inside datagrid fails to update converter when changed externally

  20. 20

    How to update AngularJS view when the value has not changed?

  21. 21

    WPF usercontrol property binding to Inotifypropertychanged, update only when value changed

  22. 22

    AngularJS : Watch scope value of a injected service changed in one directive from another directive

  23. 23

    MySQL update datetime column data only when it has changed for that record

  24. 24

    How to update element when data-* attribute changed

  25. 25

    Update React when Data in Object in Store has changed

  26. 26

    How to update search results when scope button changed. Swift UISearchController

  27. 27

    How to update md-select options when scope value changes?

  28. 28

    How to update md-select options when scope value changes?

  29. 29

    How to update UI using angularJs when scope value changes?

HotTag

Archive