我在其他rails项目中制作了角度应用程序架构:
'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: '/'
])
但它会对所有控制器发生错误。我尝试了不同的方法,但它只适用于一个文件。
Argument 'MainCtrl' is not a function, got undefined
Chrome控制台:
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")
])
角度1.2版本可能会发生变化,或者我看不出明显的错误。
答案 0 :(得分:3)
我以不同的方式命名控制器模块并将它们包含在应用程序中,我猜是很明显的。
控制器
angular.module('books', ['ngRoute', 'ngResource', 'ngCookies'])
...
应用
BookReader = angular.module('BookReader', ['ngRoute', 'home', 'main', 'exercises', 'books'])
BookReader.config(['$routeProvider', '$httpProvider', '$locationProvider', ($routeProvider, $httpProvider, $locationProvider) ->
...