AngularJs:在视图模板中具有下划线/Lodash/u

AngularJs:在视图模板中具有下划线/Lodash/u,angularjs,lodash,Angularjs,Lodash,我试图在AngularJS视图模板中提供下划线/Lodash/u。这样我就可以使用uu,如下所示: <li ng-repeat="number in _.range(100, 125)"><!-- Some logic here --></li> $scope._ = _; 但我希望有一个一次性的配置/更改,为每个视图模板的每个作用域添加 我发现一个有用的方法是: $rootScope._ = _; //Have this line in .run() m

我试图在AngularJS视图模板中提供下划线/Lodash/u。这样我就可以使用uu,如下所示:

<li ng-repeat="number in _.range(100, 125)"><!-- Some logic here --></li>
$scope._ = _;
但我希望有一个一次性的配置/更改,为每个视图模板的每个作用域添加

我发现一个有用的方法是:

$rootScope._ = _; //Have this line in .run() method.
这适用于控制器和指令的所有视图。但这不适用于独立作用域指令的视图。我必须再次在指令定义中添加($scope.\u=;)

是否有一次性/一次性更改/配置/代码可以实现这一点


注:另一个问题专门讨论在ng repeat中使用lodash。但我的问题是在每个视图模板(包括直接视图模板)中使用lodash。这就是我发现隔离作用域指令存在限制的地方。

我就是这样做的:

步骤1:在index.html中包含下划线-min.js:

<!-- underscore to be included with angular js -->
<script src="lib/underscore/underscore-min.js"></script>
第三步:在任何你喜欢的地方使用它,注射“#”:

'use strict';

angular.module('myApp')

.controller('AppCtrl', function($scope, _) {

  var test = {
    x: 10,
    name: 'Ashish Bajaj'
  }

  var 2ndTest = _.clone(test); //use underscore

});

创建一个常量并在需要时注入?对我来说,将库函数放在标记中是没有意义的,我强烈建议使用过滤器,而不是将其传递给视图。请参阅:了解更多信息。
'use strict';

angular.module('myApp')

.controller('AppCtrl', function($scope, _) {

  var test = {
    x: 10,
    name: 'Ashish Bajaj'
  }

  var 2ndTest = _.clone(test); //use underscore

});