错误:_. get不是函数

时间:2016-01-18 16:12:41

标签: javascript angularjs lodash

我正在尝试将angular-timezone-selector [on Github]实施到我现有的角度项目中。

我创建了一个解耦的working plunker here

当我尝试将其实现到我的项目中时,收到以下错误:

Error: _.get is not a function

这些是我的依赖项:

var app = angular.module('app',
  [
    'ngSanitize',
    'ui.router',
    'ui.bootstrap',
    'pascalprecht.translate',
    'tmh.dynamicLocale',
    'ngCookies',
    'angular-loading-bar',
    'google.places',
    'LocalStorageModule',
    'angular-timezone-selector'
  ]
);

这是我加载到我的html中的内容:

  <!-- jQuery first -->
  <script src="assets/bower_components/jquery/dist/jquery.min.js"></script>
  <script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
  <script src="assets/bower_components/moment/moment.js"></script>
  <script src="assets/bower_components/moment-timezone/builds/moment-timezone-with-data.js"></script>
  <script src="assets/bower_components/chosen/chosen.jquery.min.js"></script>
  <script src="assets/bower_components/lodash/lodash.min.js"></script>

  <!-- Angular specific scripts -->
  <script src="assets/bower_components/angular/angular.js"></script>
  <script src="assets/bower_components/angular-cookies/angular-cookies.js"></script>
  <script src="assets/bower_components/angular-sanitize/angular-sanitize.js"></script>
  <script src="assets/bower_components/underscore/underscore.js"></script>
  <script src="assets/bower_components/angular-resource/angular-resource.js"></script>
  <script src="assets/bower_components/angular-local-storage/dist/angular-local-storage.js"></script>
  <script src="assets/bower_components/angular-ui-router/release/angular-ui-router.js"></script>
  <script src="assets/bower_components/angular-ui-bootstrap-bower/ui-bootstrap-tpls.js"></script>
  <script src="assets/bower_components/angular-translate/angular-translate.js"></script>
  <script src="assets/bower_components/angular-translate-loader-static-files/angular-translate-loader-static-files.js"></script>
  <script src="assets/bower_components/angular-dynamic-locale/dist/tmhDynamicLocale.js"></script>
  <script src="assets/bower_components/angular-loading-bar/build/loading-bar.js"></script>
  <script src="assets/bower_components/angular-google-places-autocomplete/src/autocomplete.js"></script>
  <script src="assets/bower_components/angular-timezone-selector/angular-timezone-selector.js"></script>
  <script src="scripts/app.js"></script>

...

这是我的bower.json文件的一部分,它提供了我的资产:

...
    "moment": "2.11.1",
    "moment-timezone": "0.5.0",
    "chosen": "1.4.2",
    "lodash": "3.9.3",
    "angular-timezone-selector": "1.4.3"
...

也许这是一个lodash错误。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

  angular.module('lodash', []).factory('_', ['$window', function ($window) {
    return $window._;
  }]);

  angular.module('yourApp', [
    // angular
    'ngAnimate',
    'ngCookies',
    // angular ui
    'ui.router',
    'ui.bootstrap',
    'ui.validate',
    // third party
     'angular-loading-bar',
    'lodash'
  ]);

然后将其注入您的服务或控制器。

答案 1 :(得分:2)

请检查您使用&#39; _&#39;作为某些组件的名称(工厂,值等),因为angular-timezone-selector使用该名称创建常量: https://github.com/mishguruorg/angular-timezone-selector/blob/master/dist/angular-timezone-selector.js#L14

我希望我能提供帮助。