参数&#39; <controller>&#39;不是Angular routing </controller>中的函数

时间:2014-12-01 13:33:19

标签: javascript angularjs ionic-framework ngroute

我收到此错误,我找不到问题,以下是我的错误

Error: [ng:areq] Argument 'homeCtrl' is not a function, got undefined http://errors.angularjs.org/1.2.25/ng/areq?p0=homeCtrl&p1=not%20aNaNunction%2C%20got%20undefined at VALIDITY_STATE_PROPERTY (http://localhost:8100/lib/ionic/js/ionic.bundle.js:7703:12) at assertArg (http://localhost:8100/lib/ionic/js/ionic.bundle.js:9134:11) at assertArgFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:9144:3) at $get (http://localhost:8100/lib/ionic/js/ionic.bundle.js:14903:9) at updateView (http://localhost:8100/lib/ionic/js/ionic.bundle.js:42986:30) at IonicModule.directive.directive.compile (http://localhost:8100/lib/ionic/js/ionic.bundle.js:42942:9) at nodeLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:14336:13) at compositeLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:13730:13) at nodeLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:14330:24) at compositeLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:13730:13)

以下是我的Angular和Ionic项目:

#app.js
angular.module('ft', ['ionic', 'ft.controllers'])
.run(function($ionicPlatform) {
  // run method
})
.config(function($stateProvider, $urlRouterProvider){
  $stateProvider
  .state('app', {
    url: '/app',
    abstract: true,
    templateUrl: 'templates/sideMenu.html',
    controller: 'menuCtrl'
  })
  .state('app.home', {
    url: '/home',
    views: {
      'menuContent': {
        templateUrl: 'templates/home.html',
        controller: 'homeCtrl'
      }
    }
  });
  $urlRouterProvider.otherwise('/app/home');
});

#www/js/controllers/home_ctrl.js
angular.module('ft.controllers', [])
  .controller('homeCtrl', function($scope, $ionicModal, $timeout){

  });

#index.html file
<script src="js/app.js"></script>
<script src="js/controllers/home_ctrl.js"></script>

但是如果我从路线中移除homeCtrl,它就可以了。我现在花了相当多的时间,对我来说似乎没问题。

2 个答案:

答案 0 :(得分:1)

看起来您正在使用bundling & minification,请尝试:

var app = angular.module('ft.controllers', []);
app.controller('homeCtrl', ['$scope', '$ionicModal', '$timeout', function ($scope, $ionicModal, $timeout) {
});

缩小JS代码会替换变量名称。 AngularJS根据此名称注入模块。

答案 1 :(得分:1)

最后我能够解决问题,问题是ft.controllers用于两个控制器。

angular.module('ft.controllers', [])
  .controller('homeCtrl', function($scope, $ionicModal, $timeout){
  });

angular.module('ft.controllers', [])
.controller('menuCtrl', function($scope, $ionicModal, $timeout){
]});

当我将名称更改为ft.controllers.homeft.controllers.menu并将其添加到app.js时,它就开始工作了

我怎么认为它会起作用,因为我相信ionic sideMenu已启动的应用

#controllers.js from ionic started sideMenu app
angular.module('starter.controllers', [])

.controller('AppCtrl', function($scope, $ionicModal, $timeout) {
})

.controller('PlaylistsCtrl', function($scope) {
})

controllers.js file

app.js file

project