javascript - AngularJS UI-Router 无法识别模块上定义的 Controller

标签 javascript angularjs controller angular-ui-router

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

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

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

/** 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(定义的 Controller )的代码。

/** 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 中的注入(inject) ($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) {

关于javascript - AngularJS UI-Router 无法识别模块上定义的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25913399/

相关文章:

javascript - Jquery Mobile changePage 在 iframe 内失败

angularjs - NodeJS中,将数据库查询输出转换为Json格式

asp.net-mvc - 依赖注入(inject)到 MVC 操作方法中

session - 在 grails 中,如何获取所有当前 session 的引用?

ruby-on-rails - 没有路由匹配 { :action= >“show” , :controller= >“users” }

JavaScript 函数无法在 Chrome 和 IE 上运行,但可以在 FireFox 上运行

javascript - 自定义谷歌自定义搜索引擎。特别是更改标题字体

javascript - Canvas ,禁用图像下载

javascript - 如何使用angular中的ng class添加具有不同条件的多个类名?

css - Bootstrap btn高度与tomcat