Angular shared model with multiple controllers

I am new to Angular and trying to clear my concepts:

My app requires a single model which will be updated by different controllers. So, the model should be shared. Following link explains clearly how should I proceed with it

http://www.webdeveasy.com/angularjs-data-model/

It creates two services using factory method. The manager service checks whether the model class has been already instantiated or not. If yes it returns an already instantiated reference otherwise it new's an instance. Hence each model sees the same model instance and on updating model updates occur in all the views.

If instead of creating service using factory , one creates a service using service method which returns a new'd instance automatically, would that be shared or bound to the controller.

I assume my case cannot be achieved using service method, i must use factory method instead of service method as it new's the services everytime it is injected in any controller. Does angular not maintain some pool internally to make sure same service instance is injected in all the controllers.

I don't want an additional manager object for the model to ensure same instance being used in all the controllers


As far as your case is concerned, Both services and factory can be used, as both of them are essentially similar in nature by functionalities.

As per my understanding, you want a single news entity, to be shared across all controllers, as per singleton design pattern, ensuring one update -> updates all. This can be perfectly implemented using Angular Services.

I myself find using services easier than factory method, since the former has an easy to use syntax :D

Also, for more understanding of Difference between the two approaches, you can have a look at this answer

在这里输入图像描述

链接地址: http://www.djcxy.com/p/77986.html

上一篇: Angular ngresource更新方法不是一个函数

下一篇: 具有多个控制器的角度共享模型