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.

5
  • And what is the error that occurs? Do you expect people here to get your whole project just to reproduce the error? Commented Oct 11, 2013 at 5:52
  • Ups, sorry, updating now. Commented Oct 11, 2013 at 5:54
  • The error message says all: 'BooksCtrl' is not a function Commented Oct 11, 2013 at 6:11
  • 1
    With 1.2 you need to require ngRoute module... include the js file and add the module to your array of dependencies: ['ngCookies', 'ngResource', 'ngRoute'] Commented Oct 11, 2013 at 6:13
  • First I thought it helps, but i forgot to remove those controllers from the main file. So, it doesn't still. Commented Oct 11, 2013 at 10:00

1 Answer 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) ->
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.