AngularJS控制器解析:参数'controller'不是函数,得到了Object

时间:2013-09-24 11:10:30

标签: javascript angularjs coffeescript requirejs

我正在尝试在使用AngularJS的$ routeprovider和requireJS时将控制器注入$ routeScope

 $routeProvider.when '/',
        templateUrl : loadView('aboutJellyfish')
        controller: 'aboutJellyfish'
        resolve:
          first:loadController('aboutJellyfish')

loadController函数:

loadController = (ctrl_name) ->
        resolve = undefined
        resolve = ["$q", "$controller", "$rootScope", "$routeParams", ($q, $controller, $rootScope) ->
          deferred = undefined
          deferred = $q.defer()
          require(["../controllers/" + ctrl_name], (ctrl_module) ->
            $controllerProvider.register(ctrl_name, ctrl_module)
            $rootScope.$apply(->
              deferred.resolve not 0
            )
          )
          deferred.promise
        ]

requirejs配置:

require.config
  paths:
    jQuery: 'lib/jQuery/jquery-1.10.2.min'
    angular: 'lib/angular/angular.min'
    app: 'app'

  shim:
    'angular':
      exports : 'angular'
      deps:['jQuery']
    'app':
      deps:['angular']
      exports:'app'
    'jQuery':
      exports : '$'

  priority: ['jQuery','angular','app']
  urlArgs: 'v=1.01'

require ['angular','app'], (angular) ->
  angular.element(document).ready () ->
    angular.bootstrap(document,[applicationName])

和控制器

'use strict'
define ['app'],(app) ->
  app.controller 'aboutJellyfish', ['$scope',($scope)=>
    $scope.info='it works!'
  ]

问题是我收到以下错误'Argument'aboutJellyfish'不是函数,得到了Object'但是在Controller中使用下面的一段代码一切正常:

'use strict'
define ['app'],(app) ->
  aboutJellyfish = ["$scope", ($scope) ->
    $scope.info="Jellyfish Beta 0.1"
  ]

1 个答案:

答案 0 :(得分:1)

您的loadController('aboutJellyfish')正在返回实际的控制器对象

return resolve = [ "$q", "$controller", "$rootScope", "$routeParams", function($q, $controller, $rootScope)...

resolve {} $routeProvider需要接收函数。

resolve: {
   first: <- it needs to be a function here
}