如何使 lodash 与 Angular JS 一起工作? [英] How to make lodash work with Angular JS?
本文介绍了如何使 lodash 与 Angular JS 一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 lodash 在 ng-repeat
指令中使用它,以这种方式:
<div>你好{{n}}</div>
成为GridController
:
IndexModule.controller('GridController', function () {这._ = _})
但是它不起作用,所以没有重复.如果我将指令更改为 ng-repeat="i in [1,2,3,4,5]"
它将起作用.
lodash
已经通过 包含在
之前 <代码>角度代码>.我怎样才能让它工作?
解决方案
我更喜欢全局引入 '_' 并且可注入用于测试,请参阅我对这个问题的回答在控制器内使用下划线
var myapp = angular.module('myApp', [])//允许在控制器、单元测试中使用 DI.constant('_', window._)//在视图中使用,ng-repeat="x in _.range(3)".run(function ($rootScope) {$rootScope._ = window._;});
I'm trying to use lodash use it at ng-repeat
directives, in this way:
<div ng-controller="GridController" ng-repeat="n in _.range(5)">
<div>Hello {{n}}</div>
</div>
Being GridController
:
IndexModule.controller('GridController', function () {
this._ = _
})
However is not working and so, nothing being repeated. If I change the directive to ng-repeat="i in [1,2,3,4,5]"
it'll work.
lodash
is already included via <script>
at <header>
before angular
. How can I make it work?
解决方案
I prefer to introduce '_' globally and injectable for tests, see my answer to this question Use underscore inside controllers
var myapp = angular.module('myApp', [])
// allow DI for use in controllers, unit tests
.constant('_', window._)
// use in views, ng-repeat="x in _.range(3)"
.run(function ($rootScope) {
$rootScope._ = window._;
});
这篇关于如何使 lodash 与 Angular JS 一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文