Javascript 在 Angular 控制器中使用下划线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14968297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:30:14  来源:igfitidea点击:

Use underscore inside Angular controllers

javascriptangularjsunderscore.js

提问by Pablo

How do I use underscore library inside angularjs controllers?

如何在 angularjs 控制器中使用下划线库?

On this post: AngularJS limitTo by last 2 recordssomebody suggested to assign an _ variable to the rootScope so that the library will be available to all the scopes within the app.

在这篇文章中:AngularJS limitTo by last 2 记录有人建议为 rootScope 分配一个 _ 变量,以便该库可用于应用程序中的所有范围。

But I'm not clear where to do it. I mean should it go on the app module declaration? i.e:

但我不清楚在哪里做。我的意思是它应该放在应用程序模块声明中吗?IE:

var myapp = angular.module('offersApp', [])
            .config(['$rootScope', function($rootScope) { }

But then where do I load underscore lib? I just have on my index page the ng-app directive and script reference to both the angular-js and underscore libs?

但是我在哪里加载下划线库?我的索引页面上只有 ng-app 指令和对 angular-js 和下划线库的脚本引用?

index.html:

index.html

<head>
</head>
<body ng-app="offersApp">
...
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="scripts/vendor/angular.js"></script>
<script src="scripts/vendor/underscore.js"></script>
...  

How do I achieve this?

我如何实现这一目标?

回答by satchmorun

When you include Underscore, it attaches itself to the windowobject, and so is available globally.

当您包含 Underscore 时,它​​会将自身附加到window对象上,因此在全局范围内可用。

So you can use it from Angular code as-is.

因此,您可以按原样从 Angular 代码中使用它。

You can also wrap it up in a service or a factory, if you'd like it to be injected:

如果您希望注入它,您也可以将其封装在服务或工厂中:

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

And then you can ask for the _in your app's module:

然后你可以_在你的应用程序模块中请求:

// Declare it as a dependency of your module
var app = angular.module('app', ['underscore']);

// And then inject it where you need it
app.controller('Ctrl', function($scope, _) {
  // do stuff
});

回答by unify

I have implemented @satchmorun's suggestion here: https://github.com/andresesfm/angular-underscore-module

我在这里实施了@satchmorun 的建议:https: //github.com/andresesfm/angular-underscore-module

To use it:

要使用它:

  1. Make sure you have included underscore.js in your project

    <script src="bower_components/underscore/underscore.js">
    
  2. Get it:

    bower install angular-underscore-module
    
  3. Add angular-underscore-module.js to your main file (index.html)

    <script src="bower_components/angular-underscore-module/angular-underscore-module.js"></script>
    
  4. Add the module as a dependency in your App definition

    var myapp = angular.module('MyApp', ['underscore'])
    
  5. To use, add as an injected dependency to your Controller/Service and it is ready to use

    angular.module('MyApp').controller('MyCtrl', function ($scope, _) {
    ...
    //Use underscore
    _.each(...);
    ...
    
  1. 确保在项目中包含 underscore.js

    <script src="bower_components/underscore/underscore.js">
    
  2. 得到它:

    bower install angular-underscore-module
    
  3. 将 angular-underscore-module.js 添加到主文件 (index.html)

    <script src="bower_components/angular-underscore-module/angular-underscore-module.js"></script>
    
  4. 在您的 App 定义中添加模块作为依赖项

    var myapp = angular.module('MyApp', ['underscore'])
    
  5. 要使用,将作为注入的依赖项添加到您的控制器/服务中,它就可以使用了

    angular.module('MyApp').controller('MyCtrl', function ($scope, _) {
    ...
    //Use underscore
    _.each(...);
    ...
    

回答by wires

I use this:

我用这个:

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

See https://github.com/angular/angular.js/wiki/Understanding-Dependency-Injectionabout halfway for some more info on run.

https://github.com/angular/angular.js/wiki/Understanding-Dependency-Injection大约一半对一些更多的信息run

回答by Paul Sheldrake

You can also take a look at this module for angular

您还可以查看此模块的角度

https://github.com/floydsoft/angular-underscore

https://github.com/floydsoft/angular-underscore

回答by Nick White

If you don't mind using lodash try out https://github.com/rockabox/ng-lodashit wraps lodash completely so it is the only dependency and you don't need to load any other script files such as lodash.

如果您不介意使用 lodash,请尝试https://github.com/rockabox/ng-lodash,它完全包装了 lodash,因此它是唯一的依赖项,您不需要加载任何其他脚本文件,例如 lodash。

Lodash is completely off of the window scope and no "hoping" that it's been loaded prior to your module.

Lodash 完全不在窗口范围内,并且“不希望”它在你的模块之前被加载。

回答by jiahut

you can use this module -> https://github.com/jiahut/ng.lodash

你可以使用这个模块 -> https://github.com/jiahut/ng.lodash

this is for lodashso does underscore

这是lodash这样做underscore