0

I made angular app architecture like in other rails projects:

'use strict'

angular.module('BookReader.services', ['ngResource'])
angular.module('BookReader.directives', [])
angular.module('BookReader.controllers', ['ngCookies', 'ngRoute'])

BookReader = angular.module('BookReader', ['ngRoute', 'BookReader.controllers', 'BookReader.services', 'BookReader.directives'])

BookReader.config(['$routeProvider', '$httpProvider', '$locationProvider', ($routeProvider, $httpProvider, $locationProvider) ->
  $routeProvider
    .when '/',
      templateUrl: 'view/tour.html'
      controller: 'MainCtrl'
    .when '/home',
      templateUrl: 'view/main.html'
      controller: 'HomeCtrl'
    .when '/books',
      templateUrl: 'view/books.html'
      controller: 'BooksCtrl'
    .otherwise
      redirectTo: '/'
])

But it occurs error for all controllers. I tried different ways, but it only works in one file.

Argument 'MainCtrl' is not a function, got undefined

Chrome console:

Error: [ng:areq] http://errors.angularjs.org/undefined/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got%20undefined
    at Error (<anonymous>)
    at http://localhost:3000/assets/angular/angular.min.js?body=1:7:453
    at qb (http://localhost:3000/assets/angular/angular.min.js?body=1:19:170)
    at Ia (http://localhost:3000/assets/angular/angular.min.js?body=1:19:257)
    at http://localhost:3000/assets/angular/angular.min.js?body=1:59:137
    at http://localhost:3000/assets/angular-route/angular-route.min.js?body=1:8:9
    at http://localhost:3000/assets/angular/angular.min.js?body=1:41:424
    at s (http://localhost:3000/assets/angular-route/angular-route.min.js?body=1:7:378)
    at h.$broadcast (http://localhost:3000/assets/angular/angular.min.js?body=1:108:417)
    at http://localhost:3000/assets/angular-route/angular-route.min.js?body=1:11:443 

MainCtrl:

'use strict'

angular.module('BookReader.controllers', ['ngRoute', 'ngResource', 'ngCookies'])
  .controller('MainCtrl', ['$scope', '$http', '$cookies', ($scope, $http, $cookies) ->
    console.log("works")
  ])

Perhaps something changes in angular 1.2 version, or I can't see an obvious mistake.

4

1 回答 1

3

I named controller modules differently and included them in app, what was obvious, i guess.

Controller

angular.module('books', ['ngRoute', 'ngResource', 'ngCookies'])
...

App

BookReader = angular.module('BookReader', ['ngRoute', 'home', 'main', 'exercises', 'books'])

BookReader.config(['$routeProvider', '$httpProvider', '$locationProvider', ($routeProvider, $httpProvider, $locationProvider) ->
...
于 2013-10-11T11:08:00.190 回答