ngannotate-rails无法在开发模式下工作

时间:2017-08-15 06:00:41

标签: ruby-on-rails angularjs ruby coffeescript

我正在使用ngannotate-rails来帮助获取角度代码以压缩格式工作。这是在Rails 3.2 + Angular 1.5.x应用程序中。

我已将以下内容添加到我的Gemfile中并运行了包

group :assets do
  gem 'sprockets-derailleur', '~> 1.0.0'          # Speed up precompiles with multi-core compile
  gem 'turbo-sprockets-rails3', '~> 0.3.14'       # Compile only changed assets
  gem 'sass-rails', ' ~> 3.2'                   # Main CSS extension in use
  gem 'less', '~> 2.6.0'                          # Less required for ui-utils plugin for AngularJS
  gem 'therubyracer', '~> 0.12.1'                 # v8 library required for less
  gem 'coffee-rails'                              # Language which compiles into JS
  gem 'uglifier'                      # JS parser, minifier, compressor or beautifier toolkit

  gem 'ngannotate-rails', '~> 1.2'                # AngularJS minifier
end

然后在config / environments / development.rb中我添加了以下内容

  if ENV['NG_FORCE'] == 'true'
    config.assets.compress = true
    config.assets.compile = true
    config.assets.digest = true
    config.assets.debug = false

    # Don't fallback to assets pipeline if a precompiled asset is missed
    config.assets.precompile += %w(*.css *.js *.woff *.eot *.svg *.ttf) # The fix for font-awesome

  else
    # Do not compress assets
    config.assets.compress = false

    # Expands the lines which load the assets
    config.assets.debug = true
  end

然后我执行以下命令,但ng-annotate不注释

NG_FORCE=true RAILS_ENV=development bundle exec rake assets:clean assets:precompile:primary
NG_FORCE=true rails s

我检查public / assets /.../中的编译输出,我把它放在顶部;

(function(){var e;e=function(e,n,t,i,o,c,r,a,u,d,s,l,v,f,g,h,m,p,b,k,y,I,T,E,q,w,A){var D,P,F,x,j;

它已缩小但未使用字符串注释,因此AngularJS会抛出此错误;

application-57db964b9323e90284f1f3c331c9c3aa.js:43 Error: [$injector:unpr] Unknown provider: eProvider <- e <- InvoicesController
http://errors.angularjs.org/1.5.7/$injector/unpr?p0=eProvider%20%3C-%20e%20%3C-%20InvoicesController
    at http://localhost:3000/assets/application-57db964b9323e90284f1f3c331c9c3aa.js:41:28591
    at http://localhost:3000/assets/application-57db964b9323e90284f1f3c331c9c3aa.js:42:16654
    at Object.r [as get] (http://localhost:3000/assets/application-57db964b9323e90284f1f3c331c9c3aa.js:42:15610)
    at http://localhost:3000/assets/application-57db964b9323e90284f1f3c331c9c3aa.js:42:16739
    at r (http://localhost:3000/assets/application-57db964b9323e90284f1f3c331c9c3aa.js:42:15610)
    at o (http://localhost:3000/assets/application-57db964b9323e90284f1f3c331c9c3aa.js:42:15910)
    at Object.s [as instantiate] (http://localhost:3000/assets/application-57db964b9323e90284f1f3c331c9c3aa.js:42:16247)
    at http://localhost:3000/assets/application-57db964b9323e90284f1f3c331c9c3aa.js:43:9470
    at http://localhost:3000/assets/application-57db964b9323e90284f1f3c331c9c3aa.js:98:32136
    at Object.<anonymous> (http://localhost:3000/assets/application-57db964b9323e90284f1f3c331c9c3aa.js:47:31053) <div id="wrapper" class="ui-wrapper ng-scope" ui-view="">

更新

InvoicesController

InvoicesController = ($scope, $http, $location, $state, $stateParams, $uibModal, $timeout, flash, Invoice, Division, Costing, Job, JobService, Common, ShortcutService, InvoiceService, TimesheetService, DisbursementService, DebtorService, SearchService, LineItemService, DateService, $filter, $mdDialog, $mdMedia, Restangular, hotkeys) ->

<Snip: Too long>
  switch $state.current.name
    # Index

    when "invoices"
      $scope.query = SearchService.getFilter('invoices')
      page = SearchService.getPage('invoices')

      if ($scope.query)
        # Filter already exists
        $scope.filterForm = true
        getInvoices(angular.extend({}, $scope.query, {page: page, limit: 10, search: $scope.query}))
      else if page > 0
        $scope.query = { order: '-invoice_no', limit: 10,  page: page }
        $scope.onPaginate(page, 10)
      else
        $scope.resetFilter()

  <Snip>

angular
  .module('paisApp')
  .controller('InvoicesController', InvoicesController)

1 个答案:

答案 0 :(得分:1)

虽然上面的代码段可能已被切断,但我看不到您在代码中提供“ngInject”指令的位置。

我希望您的控制器代码看起来像

MyController = ($scope, ...) ->
  "ngInject"
  # etc

在转换代码时,ng-annotate会将"ngInject"视为点中的钩子。