它是实践的角度指令请求数据坏 [英] Is it bad practice for an angular directive to request data

查看:152
本文介绍了它是实践的角度指令请求数据坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,考虑一个的currentUser指令。

Consider for example a currentUser directive.

我可以让控制器使用服务来获取有关当前用户的数据,它提供的指令,让指令使一些你好{user.name}的模板。

I could let the controller use a service to get the data about the current user, provider it to the directive and let the directive render some "hello {user.name}" template.

我也可以有指令,有一些currentUserService的依赖,并在指令的控制器要求currentUserService.getCurrentUser。

Alternatively I could have the directive have a dependency on some currentUserService and in the directive's controller ask for currentUserService.getCurrentUser.

时的两个中的一个显著优于其他任何原因?
我倾向于去的第一选择,但如果使用第二个就不用其周围目前所有的用户逻辑更低的S $ P $垫的好处不知道...

Is one of the two significantly better then the other for any reason? I tend to go with the first option but not sure if the using the second one would not have a benefit of having all the current-user-logic less spread around...

感谢

推荐答案

只要你从一个服务请求数据,我相信有一个指令,向它的依赖是好的。结果
控制器的主要方面是具有存取$范围,而不是多

As long as you're requesting the data from a service, I believe having a dependency toward it in a directive is fine.
The main aspect of the controller is having access to $scope, not much more.

这篇关于它是实践的角度指令请求数据坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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