小编典典

如何使lodash与Angular JS一起使用?

angularjs

我正在尝试使用lodash ng-repeat以这种方式在指令中使用它:

<div ng-controller="GridController" ng-repeat="n in _.range(5)">
    <div>Hello {{n}}</div>
</div>

存在GridController

IndexModule.controller('GridController', function () {

this._ = _

})

但是不起作用,因此,没有任何 重复 。如果我将指令更改为ng-repeat="i in [1,2,3,4,5]"它将起作用。

lodash 通过已经包含<script><header>之前 angular 。我该如何运作?


阅读 325

收藏
2020-07-04

共1个答案

小编典典

我更喜欢在全局范围内引入“ _”并且可以注入以进行测试。

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._;
  });
2020-07-04