【问题标题】:AngularJS UI-Router won't recognize a defined controller on the moduleAngularJS UI-Router 无法识别模块上定义的控制器
【发布时间】:2014-09-18 21:44:06
【问题描述】:

我正在学习 Angular,我正在尝试使用 ui-router 使用控制器设置状态。我有一个名为 app.js 的主模块,然后是基于不同内容的子模块。

第一个子模块是 diary.js。除了控制器之外,一切都在使用这个控制器。状态在UI等中起作用。当我直接在diary.js中制作控制器时(例如控制器:function(){//stuff},它工作正常)。但是当我尝试为日记状态包含一个已经定义的控制器时(就像它当前编写的那样),我收到以下错误(我无法发布整个错误,因为 stackoverflow 不会让我拥有那么多链接):

“错误:[ng:areq] errors.angularjs.org/1.2.23/ng/areq?p0=DiaryCtrl&p1=not%20aNaNunction%2C%20got%20undefined 在错误(本机)...

/** diary.js */
'use strict';

(function () {

angular.module('diary', ['ui.router'])  

    .config(['$stateProvider', function ($stateProvider) {

        // States
        $stateProvider.state('diary', {
            url         : '/diary',
            templateUrl : 'diary/html/diary.html',
            controller  : 'DiaryCtrl'
        });
    }]);

}).call();

这是 DiaryCtrl.js(定义的控制器)的代码。

/** DiaryCtrl.js */
'use strict';

angular.module('diary')

    .controller('DiaryCtrl', [$scope, $http, function ($scope, $http) {

        $http.get('api/json/diaries.html').success(function (data) {
            $scope.diaries = data;
        }).error(function (status) {
            console.log(status);
        });

    }]);

如果有任何帮助,我将不胜感激。如果您需要更多信息,请告诉我。

【问题讨论】:

  • 您确定包含 DiaryCtrl.js 脚本吗?
  • 不。就是这样。菜鸟失误。谢谢!

标签: javascript angularjs controller angular-ui-router


【解决方案1】:

我相当肯定这是因为您在DiaryCtrl 中的注入($scope$http)不是字符串:

.controller('DiaryCtrl', [$scope, $http, function ($scope, $http)

应该是:

// Notice the added quotations around the scope and http injections in the array
.controller('DiaryCtrl', ['$scope', '$http', function ($scope, $http) {

【讨论】:

  • 这是因为我实际上并没有在我的索引页面上包含控制器。愚蠢的错误。但你是对的,我应该用引号括起来,所以我也修复了它。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多