angularjs bind service array length property to controller scope

kannix

I'm trying to bind the array length from an other service to my controller scope like this:

app.controller('TestCtrl', function ($scope, safePostService) {
    $scope.count = safePostService.queue.length;

    $scope.addOne = function () {
        safePostService.post('/echo/json/', {
            json: JSON.stringify({
                text: 'some text',
                array: [1, 2, 'three'],
                object: {
                    par1: 'another text',
                    par2: [3, 2, 'one'],
                    par3: {}
                }
            }),
            delay: 3
        });
    };
});

Thats my service:

app.service('safePostService', function ($http, $timeout) {
    var self = this;

    var syncIntervall = 1000;
    var timeoutPromise;
    var sending = false;

    this.queue = [];

    this.post = function (url, data) {
        self.queue.push({
            url: url,
            data: data
        });
        syncNow();
    };

    function syncNow() {
        $timeout.cancel(timeoutPromise);
        syncLoop();
    }

    function syncLoop() {
        if (sending) return;
        sending = true;
        var item = self.queue[0];
        $http.post(item.url, item.data).
        success(function () {
            self.queue.shift();
            afterResponse(true);
        }).
        error(function () {
            afterResponse(false)
        });
    }

    function afterResponse(success) {
        sending = false;
        if (self.queue.length > 0) {
            timeoutPromise = $timeout(syncLoop, (success) ? 50 : syncIntervall);
        }
    }
});

The $scope.count keeps showing 0 and doesn't get updated.

Here is a fiddle: http://jsfiddle.net/kannix/CGWbq/

Paul Rad

Angular should $watch the safePostService.queue.

Try:

$scope.$watch(function() { return safePostService.queue.length; }, function(item) {
  $scope.count = item;
}, true);

Fiddle: http://jsfiddle.net/CGWbq/4/

You decrease the queue array in case of success :

self.queue.shift();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angularjs service callback to update scope of controller

From Dev

Pass scope to Service on AngularJS

From Dev

how do i pass scope from controller to service in angularjs?

From Dev

AngularJS Bind service array variable to controller scope

From Dev

AngularJS: Bind angular service property to scope

From Dev

angularjs initiating controller scope from a service

From Dev

Is it ok to bind whole service to controller scope?

From Dev

bind $scope in Directives/Controller

From Dev

Angularjs "Controller as" or "$scope"

From Dev

Angular bind service property to a controller scope

From Dev

AngularJS: Bind a directive to a Controller via a Service update

From Dev

How to bind properly to a service property in AngularJS?

From Dev

AngularJS - refactoring controller into service - understanding self/this and scope

From Dev

Not able to lazily update a controller scope with a REST service - Angularjs

From Dev

AngularJS Bind service array variable to controller scope

From Dev

Angular bind controller to new scope

From Dev

angularjs bind service array length property to controller scope

From Dev

Parent Controller's scope vs Shared Service in AngularJS?

From Dev

Bind a controller $scope to objects in service

From Dev

Why doesn't AngularJS service bind to scope?

From Dev

angularjs fill scope variable with REST service in existing controller

From Dev

AngularJS $scope.foo is set with service, but later on in the same controller undefined

From Dev

Angularjs - Passing the $scope to a controller

From Dev

bind $scope in Directives/Controller

From Dev

Access scope property in controller with AngularJS

From Dev

understanding the controller scope in angularJS

From Dev

Using AngularJS service to update scope in other controller from other module

From Dev

Bind a function in service to $scope (Error: Cannot set property 'onChange' of undefined)

From Dev

angularjs bind to controller with isolated scope

Related Related

  1. 1

    Angularjs service callback to update scope of controller

  2. 2

    Pass scope to Service on AngularJS

  3. 3

    how do i pass scope from controller to service in angularjs?

  4. 4

    AngularJS Bind service array variable to controller scope

  5. 5

    AngularJS: Bind angular service property to scope

  6. 6

    angularjs initiating controller scope from a service

  7. 7

    Is it ok to bind whole service to controller scope?

  8. 8

    bind $scope in Directives/Controller

  9. 9

    Angularjs "Controller as" or "$scope"

  10. 10

    Angular bind service property to a controller scope

  11. 11

    AngularJS: Bind a directive to a Controller via a Service update

  12. 12

    How to bind properly to a service property in AngularJS?

  13. 13

    AngularJS - refactoring controller into service - understanding self/this and scope

  14. 14

    Not able to lazily update a controller scope with a REST service - Angularjs

  15. 15

    AngularJS Bind service array variable to controller scope

  16. 16

    Angular bind controller to new scope

  17. 17

    angularjs bind service array length property to controller scope

  18. 18

    Parent Controller's scope vs Shared Service in AngularJS?

  19. 19

    Bind a controller $scope to objects in service

  20. 20

    Why doesn't AngularJS service bind to scope?

  21. 21

    angularjs fill scope variable with REST service in existing controller

  22. 22

    AngularJS $scope.foo is set with service, but later on in the same controller undefined

  23. 23

    Angularjs - Passing the $scope to a controller

  24. 24

    bind $scope in Directives/Controller

  25. 25

    Access scope property in controller with AngularJS

  26. 26

    understanding the controller scope in angularJS

  27. 27

    Using AngularJS service to update scope in other controller from other module

  28. 28

    Bind a function in service to $scope (Error: Cannot set property 'onChange' of undefined)

  29. 29

    angularjs bind to controller with isolated scope

HotTag

Archive