0

My application has two execution paths.

Root
 |
 |---Tool1 --> T1Page 1
 |         --> T1Page 2
 |
 |---Tool2 --> T2Page 1
 |         --> T2Page 2
 |
 |----Factory (F)

Here T1Page2 and and T2Page2 are having similar UI elements and I have defined a factory for that. Now the problem is, if user first start Tool1 and then navigate to Tool2, in T2Page2 it displays the values selected in T1Page2 . This is obviously because of the singleton injection in the controller. How can i make sure that Angular returns a new factory object when user comes to T2Page2.

One solution I can think of is to add a clean up method in the factory and call it whenever user starts the T2Page2. But that doesnt sounds good to me.

Factory

(function () {
var serviceId = 'setUp';
angular.module('app').factory(serviceId , ['common', 'datacontext']);
function setUp(common,datacontext){
var vm = this;
vm.name='';
vm.address='';
return init();

 function init() {
      return vm;
  }
}
}());

T1Page2 Controller 1

(function () {    
    var controllerId = 'T1Page2';
    angular.module('app').controller(controllerId, ['common', 'datacontext', 'setUp', T1Page2]);

function T1Page2(common, datacontext, setup) {
   //code omitted
  }
}());

T2Page2 Controller 2

(function () {    
    var controllerId = 'T2Page2';
    angular.module('app').controller(controllerId, ['common', 'datacontext', 'setUp', T2Page2]);

function T2Page2(common, datacontext, setup) {
   //need a new setUp object here. 
   // rest of the code omitted
  }
}());
John Slegers
  • 45,213
  • 22
  • 199
  • 169
RasikaSam
  • 5,363
  • 6
  • 28
  • 36

1 Answers1

0

I'd say there are two possible solutions to your question.

Solution 1

Use an Angular Service rather than a Factory. While a factory maintains a persistent state (it is new'ed when the module is loaded), a service returns a constructor function for you to new as many times as you'd like (you can create a new service for each Tool).

Sample Service:

angular.module('app').service('MyService',
  function () {
    this.name = '';
    this.address = '';
  }
]);

Utilizing a Service within a controller (for example)

$scope.myService = new MyService();

Solution 2

Create 2 factories. One factory for Tool1 and another for Tool2.

Solution 2 might be easier in your case since you want a service (used in the general sense here) to be used by one group of pages, while a second group of pages needs a different persistent state object.

sfletche
  • 47,248
  • 30
  • 103
  • 119
  • @sflectche thanks. I will try first solution, otherwise i will be duplicating lots of code. – RasikaSam Jul 03 '15 at 04:28
  • if i dont inject and use $scope.service = new MyService()'; it says myService is not defined. If I inject myService, it says "Object is not a function". – RasikaSam Jul 05 '15 at 23:37
  • Sorry @JenorD - I had a typo. You were correct, it should be `$scope.service`. I just updated my answer. – sfletche Jul 05 '15 at 23:58
  • 1
    `service` and `factory` are the other way around. A service is instantiated once and shared everywhere it is injected; a factory is a function that is invoked whenever you need a new instance. – Sly_cardinal Jul 06 '15 at 00:06
  • Hey @Sly_cardinal - I do think there is a lot of confusion about this (and [the docs](https://docs.angularjs.org/guide/providers) don't exactly help), this [stackoverflow answer](http://stackoverflow.com/questions/14324451/angular-service-vs-angular-factory) provides a good explanation about the service being new'ed and the factory being invoked. – sfletche Jul 06 '15 at 00:17
  • @sflectche, it says all providers in AngularJS (value, constant, services, factories) are singletons? So I will end up in the same situation even if I use the service. If user opened Tool1Page2, and then navigate back to Tool2Page2, then I will get the same object in Tool1Page2 injected in Tool2Page2. Isnt that so? – RasikaSam Jul 07 '15 at 01:45
  • When you `new` a service, you are creating a fresh and distinct copy of that service. Each `new`'ed service will contain a state that is distinct from other `new`'ed instances of that same service. – sfletche Jul 07 '15 at 02:32