te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Attaching underscore or lodash to angular - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Attaching underscore or lodash to angular - Stack Overflow

programmeradmin3浏览0评论

Is it ok to attach underscore.js variable to angular variable? so I can call underscore like: angular._ ? Since underscore is less likely to be mocked at testing and we can't declare global variables?

if so, which part of my angular.js application should I add it?

Is it ok to attach underscore.js variable to angular variable? so I can call underscore like: angular._ ? Since underscore is less likely to be mocked at testing and we can't declare global variables?

if so, which part of my angular.js application should I add it?

Share Improve this question edited Jan 14, 2020 at 21:38 SoEzPz 15.9k8 gold badges64 silver badges66 bronze badges asked Jul 9, 2015 at 8:53 Louie AlmedaLouie Almeda 5,63234 silver badges41 bronze badges 7
  • just angular._ = _; – dfsq Commented Jul 9, 2015 at 9:10
  • 1 Yes it's easy to set it, but my question is: is it ok? :) – Louie Almeda Commented Jul 9, 2015 at 9:16
  • Isn't angular a global variable? Can't you treat _ the same? – Davin Tryon Commented Jul 9, 2015 at 9:17
  • Yes, it's ok. Does it make sense? - not sure. Just use _ as is or wrap it in the service like utils which basically exposes window._. – dfsq Commented Jul 9, 2015 at 9:23
  • Wouldn't it be better to create a wrapper service for underscore that you can just inject where needed? – Nat Wallbank Commented Jul 14, 2015 at 9:19
 |  Show 2 more ments

6 Answers 6

Reset to default 11

I prefer to create a wrapper service in it's own injectable module like such:

angular.module('underscore.service', [])
.factory('_', function () {
  return window._; // assumes underscore has already been loaded on the page
});

As noted, you should include underscore.js before angular in your html as you typically would.

This approach allows makes underscore accessible in a testing environment.

I think it would be better not attaching underscore to angular but use it directly.

The best way I have seen to do this is dependency injecting it.

check out this link to ng-underscore link

or

this link angular-underscore link

If you use ES6 modules you can just import it, if you use the iife approach you can register it as a constant and later inject it

angular.module("app").constant("_", _);

Still I would remend an es6 architecture. You can take a look at this

What testing suite are u going to be using?

If u are using karma, u can add any third party lib in the karma config under the files option. These files will be available in the browser and accessible to your tests. No need to wrap the lib in an angular service or attach it to angular.

If you whant to use underscore you can just simply add it to your scope

Do the following in you controller

$scope._ = _;

now you use all underscore features inside you html template like

<div ng-repeat="value in _.filter(list, ...)"></div>
发布评论

评论列表(0)

  1. 暂无评论