gpt4 book ai didi

javascript - 如何让 lodash 与 Angular JS 一起工作?

转载 作者:IT王子 更新时间:2023-10-29 02:52:17 25 4
gpt4 key购买 nike

我正在尝试使用 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 。我怎样才能让它发挥作用?

最佳答案

我更喜欢全局引入'_'并可注入(inject)测试,请参阅我对这个问题的回答 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._;
});

关于javascript - 如何让 lodash 与 Angular JS 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23862119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com