通过电流范围的AngularJS服务 [英] Passing current scope to an AngularJS Service

查看:132
本文介绍了通过电流范围的AngularJS服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是不是正确传递当前<$​​ C $ C> $范围来的AngularJS服务?

Is it correct to pass the "current" $scope to an AngularJS service?

我在我有一个$服务并不知道它只有一个控制器消耗的情况,我想有在$服务方法到控制器的范围参考自己。

I'm in the situation where I've a $service knowing it's consumed by only one controller, and I'd like to have a reference to the controller's scope in the $service methods themselves.

是不是的哲学的是否正确?

还是我最好的广播事件到$ rootScope,然后让我的控制器,听取他们的意见?

Or I'd better to broadcast events to the $rootScope and then make my controller listen to them?

推荐答案

,让控制器知道什么事情发生异步,使用的角承诺的。

To let the controller know when something async happens, use Angular promises.

要挑起 $适用,你并不需要的范围,你可以叫 $ rootScope。$适用,因为没有区别调用它在一个特定的范围或根。

To provoke the $apply, you don't need the scope, you can call $rootScope.$apply, as there is no difference calling it in a specific scope or in the root.

关于变量读,如果你收到的参数,它会更好。但你也可以从一个范围作为对象参数读取它,但我会去的参数,这将使你的服务接口更清晰。

Regarding the variable reading, it would be better if you received parameters. But you could also read it from a scope as an object parameter, but I would go with parameter, that would make your service interface much more clearer.

这篇关于通过电流范围的AngularJS服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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