在AngularJS中放置一些小型实用函数的好地方在哪里? [英] Where is a good place to put a few small utility functions in AngularJS?

查看:105
本文介绍了在AngularJS中放置一些小型实用函数的好地方在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用的功能很少,例如:

I have a very small number of functions that I need to use like this:

data-ng-disabled="_isEmpty(grid.view) || view != 'preview'"

我所做的是:

$scope._isEmpty = _.isEmpty;

,因此_isEmpty实际上是lodash函数.

so that the _isEmpty is actually the lodash function.

这是合理的做法吗?如果是这样的话,那对我来说是个好地方 编码上面的行?我应该在我的appController中编写代码.我也应该附上 ._isEmpty是$ scope还是$ rootscope?我听说有人谈论提供服务,然后注入这种服务.但是对于几行代码来说,这似乎有些过头了.

Is this a reasonable thing to do? Also if so then where would be a good place for me to code the above line? Should I code that in my appController. Also should I attach the ._isEmpty to $scope or $rootscope? I have heard people talking about making a service and then injecting this. But for a few lines of code this seems overkill.

推荐答案

尽管通常不建议这样做,但我可以看到将函数放到rootscope中可以解决这种问题.然后,不再需要将行$scope._isEmpty = _.isEmpty;放入每个控制器中.对我来说,更好的方法是将实用程序功能重新编码为指令,即使涉及某些编码也是如此.解决问题的另一种方法是使用服务.

Though generally not recommended, I could see this kind of problem solved with putting the functions into the rootscope. Then putting the line $scope._isEmpty = _.isEmpty; in every controller would not be necessary anymore. A far better way for me would be to recode the utility functions into directives, even if involves some coding. Another way to solve the problem is to use services.

这篇关于在AngularJS中放置一些小型实用函数的好地方在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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