1

If I assign an object in a factory like this:

$http.get('/api/whatever').success(function(data) {
  result = data.obj
});

It gives result a new address and therefore the reference from my controller to the result is broken. How to assign it then? Value by value works, but seems not the right way. How to watch an object properly?

Stefan
  • 1,041
  • 1
  • 14
  • 28
  • 2
    Quick and dirty ~ `angular.extend(result, data.obj)` – Phil May 14 '15 at 01:27
  • I generally recommend that you return a promise from the service. Then you can resolve the promise in the controller and grab the value. – TGH May 14 '15 at 01:31
  • Do you mean you are sharing a global `result` variable between your factory and controller? *Outch* – floribon May 14 '15 at 01:53

1 Answers1

0

Basically factory has get and set methods for getting and setting the value in factory object. Here is the example. Some time you may have to use events after updating the factory object so all others instance have updated value

Community
  • 1
  • 1
Bharat Bhushan
  • 2,077
  • 2
  • 21
  • 34