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

jhamm

I have basically created a cache in my Angular service. If the REST call has already been made, the an array is returned, else it makes the REST call to get the needed info. Here is my service:

define(function(require) {

var module = require('portal/js/services/app.services');

return module.factory('providerService', function($resource) {
  return {
    requestingProviders: [],
    getRequestingProviders: function() {
      var that = this;
      if(this.requestingProviders.length === 0) {
        this.getResource().search({
          role: 'REQUESTING_PROVIDER'
        }, function(data) {
          that.requestingProviders = data.providers;
          return that.requestingProviders;
        });
      } else {
        return that.requestingProviders;
      }
    },
    getResource: function() {
      return $resource('/api/v1/providers/:providerId', {providerId:'@id'}, {
        search: {
          method: 'GET',
          headers: {
            'RemoteUser': 'jhornsby',
            'Content-Type': 'application/json'
          },
          params: {
            limit: 2000,
            organizationId : '0001194'
          }
        }
      });
    }
  };
};

Here is an example of me accessing the service from a controller:

define(function(require) {
  var module = require('portal/js/controllers/app.controller');

  module.controller('AddPatientCtrl', function($scope, providerService) {
   $scope.providers = providerService.getRequestingProviders();
  };

  return module;
});

When the REST call returns, it does not update my scope.providers variable in my controller. When I try to access it a 2nd time, then it works fine by accessing the array instead of calling out to the REST service. WHy won't my scope update in my controller during the REST call?

Kia Panahi Rad

The prpblem is that you are calling an async get method

You'll have to assign the $scope in a callback function from the service (or in this case the factory) to make sure that it will take the desired value.

As you have said, in the first time $scope is assined to something that has not been yet resolved but in subsequent calls that parameter is resolved so $scope will get the proper value.

I hope it helped.

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 service array length property to controller scope

From Dev

angularjs - Watching service properties in a controller scope... changing them from a directive... no update?

From Dev

Angularjs is not able to find my controller

From Dev

Is $scope a real service in AngularJS

From Dev

angularjs initiating controller scope from a service

From Dev

Angularjs "Controller as" or "$scope"

From Dev

Update to $scope object in AngularJS Service does not update View

From Dev

My AngularJS view is able to access it's controller's scope, but ng-repeat doesn't work

From Dev

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

From Dev

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

From Dev

AngularJS service update controller variable

From Dev

AngularJS Bind service array variable to controller 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

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

AngularJS Controller to update $scope automatically when backend data changes

From Dev

Not able to invoke the API Services when something is between Controller and Service in AngularJS?

From Dev

How to make 2 different controller update and retrieve common data using scope variable and .Service getter setter method in angularjs

From Dev

My AngularJS view is able to access it's controller's scope, but ng-repeat doesn't work

From Dev

AngularJS: Modifying Service within Service doesn't update Controller

From Dev

understanding the controller scope in angularJS

From Dev

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

From Dev

Not able to inject Service in Controller in typescript

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 service array length property to controller scope

  6. 6

    angularjs - Watching service properties in a controller scope... changing them from a directive... no update?

  7. 7

    Angularjs is not able to find my controller

  8. 8

    Is $scope a real service in AngularJS

  9. 9

    angularjs initiating controller scope from a service

  10. 10

    Angularjs "Controller as" or "$scope"

  11. 11

    Update to $scope object in AngularJS Service does not update View

  12. 12

    My AngularJS view is able to access it's controller's scope, but ng-repeat doesn't work

  13. 13

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

  14. 14

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

  15. 15

    AngularJS service update controller variable

  16. 16

    AngularJS Bind service array variable to controller 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

    angularjs fill scope variable with REST service in existing controller

  20. 20

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

  21. 21

    Angularjs - Passing the $scope to a controller

  22. 22

    AngularJS Controller to update $scope automatically when backend data changes

  23. 23

    Not able to invoke the API Services when something is between Controller and Service in AngularJS?

  24. 24

    How to make 2 different controller update and retrieve common data using scope variable and .Service getter setter method in angularjs

  25. 25

    My AngularJS view is able to access it's controller's scope, but ng-repeat doesn't work

  26. 26

    AngularJS: Modifying Service within Service doesn't update Controller

  27. 27

    understanding the controller scope in angularJS

  28. 28

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

  29. 29

    Not able to inject Service in Controller in typescript

HotTag

Archive