【问题标题】:Lodash and underscore.string into AngularJS applicationLodash 和 underscore.string 进入 AngularJS 应用程序
【发布时间】:2014-11-21 11:45:33
【问题描述】:

问题很简单,如何在AngularJS项目中成功实现Lodash和underscore.string库?

有必要使用RequireJS来实现吗?

【问题讨论】:

    标签: angularjs lodash underscore.string.js


    【解决方案1】:

    你可以使用angular-underscore

    请看这里https://github.com/floydsoft/angular-underscore

    或角度-lodash

    https://github.com/cabrel/angular-lodash

    【讨论】:

      【解决方案2】:

      感谢@sylwester 的回复我想通了,但他的回答只是一个提示,所以我决定再丰富一点。

      有必要使用RequireJS来实现吗?

      不,您不需要使用 RequireJS 将这两个库一起实现到 AngularJS 项目中。但是很容易实现——看看这个article。

      没有RequireJS的实现

      1. 使用angular-lodash 或angular-underscore(取决于您的实现方式)
      2. 然后将underscore.string下载到你的项目中:

        $ bower install underscore.string --save-dev
        
      3. 最后确保将库添加到您的index.html:

        <script type="text/javascript" src="vendor/loadash/lodash.js"></script>
        <script type="text/javascript" src="vendor/angular/angular.js"></script>
        <script type="text/javascript" src="vendor/angular-loadash/angular-loadash.js"></script>
        <script type="text/javascript" src="vendor/underscore.string/underscore.string.js">
        

      请随意使用 RequireJS 改进此答案。

      【讨论】:

      • 你能告诉我在你上面写的js文件被加载之后,如何从underscore.string实际调用一个函数吗?
      • 看看this article。我在帖子中提到过。
      【解决方案3】:

      创建此服务,现在您可以在 underscore.js 中使用 underscore.string

      类似:_.capitalize('hello') --> 'Hello'

      angular.module('underscore', []).factory('_', ['$window', function($window) {
          $window._.mixin(s.exports());
          return $window._;
      }]);
      

      【讨论】:

        猜你喜欢
        • 2015-02-15
        • 1970-01-01
        • 2019-02-19
        • 1970-01-01
        • 2015-02-07
        • 2022-01-27
        • 2016-04-06
        • 2017-06-22
        • 2012-12-09
        相关资源
        最近更新 更多