多个控制器共享角模型 [英] Angular shared model with multiple controllers

查看:176
本文介绍了多个控制器共享角模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的角,并试图清除我的概念:

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.

如果不是使用工厂,一是创建一个使用服务方法的服务,它返回一个<$ C创建服务$ C> new'd 例如自动,将是共享或绑定到控制器。

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.

我认为不能用服务方法,我必须使用工厂方法,而不是<$ C来实现我的情况$ C>服务方法,因为它新的服务,每次它在任何控制器注入。难道没有棱角保持一定池内部,以确保相同的服务实例在所有控制器注入。

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 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.

我觉得自己更容易使用比服务工厂方法,因为前者具有一个易于使用的语法:D

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

这篇关于多个控制器共享角模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆