简体   繁体   English

注入控制器时出现角度模块注入错误

[英]Angular Module injection error while injecting controllers

I am new to AngularJS and facing a problem injecting controller to an angular app. 我是AngularJS的新手,并且在将控制器注入角度应用程序时遇到问题。 I am trying to build a single page application where the user can navigate through the pages of a particular application form. 我正在尝试构建一个页面应用程序,用户可以在其中浏览特定应用程序表单的页面。 While the code for the controllers was in the same JS file, it was working fine. 虽然控制器的代码在同一个JS文件中,但工作正常。 But moving the controllers to a separate file(as in this case, 'js/controllers/pagecontroller.js') is throwing the error below. 但是将控制器移动到一个单独的文件(在这种情况下为'js / controllers / pagecontroller.js')会在下面抛出错误。

Error: 错误:

Uncaught Error: [$injector:nomod] http://errors.angularjs.org/1.5.5/$injector/nomod?p0=uwApp.controllers
Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.5/$injector/modulerr?p0=uwApp&p1=Error%3A%2…oogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.5.5%2Fangular.min.js%3A21%3A19)

index.html: index.html的:

<!DOCTYPE html>
<html ng-app="uwApp">
    <head>

        <!-- SCROLLS -->
        <!-- load bootstrap and fontawesome via CDN -->
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.css" />

        <!-- SPELLS -->
        <!-- load angular and angular route via CDN -->
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-route.js"></script>

        <!-- Donut chart api -->
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>

        <script src="js/app.js"></script>
        <script src="js/controllers/maincontroller.js"></script>
        <script src="js/controllers/pagecontroller.js"></script>
        <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body ng-controller="mainController">
        <div ng-view></div>
    </body>
</html>

app.js app.js

'use strict';
    var uwApp = angular.module('uwApp', ['uwApp.controllers','ngRoute'])
    .config(function($routeProvider) {

        $routeProvider

            // route for the home page
            .when('/', {
                templateUrl : 'pages/home.html',
                controller  : 'mainController'
            })

            // route for the page
            .when('/acknandagreement', {
                templateUrl : 'pages/loan/acknandagreement.html',
                controller  : 'pageCtrl9'
            })

            // route for the page
            .when('/assetsandliabilities', {
                templateUrl : 'pages/loan/assetsandliabilities.html',
                controller  : 'pageCtrl6'
            })

            // route for the page
            .when('/borrowerinfo', {
                templateUrl : 'pages/loan/borrowerinfo.html',
                controller  : 'pageCtrl3'
            })

            // route for the page
            .when('/employmentinfo', {
                templateUrl : 'pages/loan/employmentinfo.html',
                controller  : 'pageCtrl4'
            })

            // route for the page
            .when('/infoandgovpurpose', {
                templateUrl : 'pages/loan/infoandgovpurpose.html',
                controller  : 'pageCtrl10'
            })

            // route for the page
            .when('/monthlyincomeandche', {
                templateUrl : 'pages/loan/monthlyincomeandche.html',
                controller  : 'pageCtrl5'
            })

            // route for the page
            .when('/morttypeandterm', {
                templateUrl : 'pages/loan/morttypeandterm.html',
                controller  : 'pageCtrl1'
            })

            // route for the page
            .when('/propertyinfoandpurpose', {
                templateUrl : 'pages/loan/propertyinfoandpurpose.html',
                controller  : 'pageCtrl2'
            })

            // route for the page
            .when('/residualapplication', {
                templateUrl : 'pages/loan/residualapplication.html',
                controller  : 'pageCtrl11'
            })

            // route for the page
            .when('/txndetails', {
                templateUrl : 'pages/loan/txndetails.html',
                controller  : 'pageCtrl7'
            })

            // route for the page
            .when('/declarations', {
                templateUrl : 'pages/loan/declarations.html',
                controller  : 'pageCtrl8'
            })
            ;
    });

pagecontroller.js: pagecontroller.js:

'use strict';

angular.module('uwApp.controllers')
.controller('pageCtrl1', function($scope) {
    $scope.page = 'morttypeandterm';
})

.controller('pageCtrl2', function($scope) {
    $scope.page = 'propertyinfoandpurpose';
})

.controller('pageCtrl3', function($scope) {
    $scope.page = 'borrowerinfo';
})

.controller('pageCtrl4', function($scope) {
    $scope.page = 'employmentinfo';
})

.controller('pageCtrl5', function($scope) {
    $scope.page = 'monthlyincomeandche';
})

.controller('pageCtrl6', function($scope) {
    $scope.page = 'assetsandliabilities';
})

.controller('pageCtrl7', function($scope) {
    $scope.page = 'txndetails';
})

.controller('pageCtrl8', function($scope) {
    $scope.page = 'declarations';
})

.controller('pageCtrl9', function($scope) {
    $scope.page = 'acknandagreement';
})

.controller('pageCtrl10', function($scope) {
    $scope.page = 'infoandgovpurpose';
})

.controller('pageCtrl11', function($scope) {
    $scope.page = 'residualapplication';
});

maincontroller.js maincontroller.js

'use strict';
angular.module('uwApp.controllers', [])
 .controller('mainController', function($scope) {

});

I have no clue why its not working. 我不知道为什么它不起作用。 Please suggest. 请提出建议。

Update 更新

I made a working example on plnkr from supplied code. 我通过提供的代码在plnkr上做了一个工作示例 There are a couple things I changed: 我改变了几件事:

1) Since your base route will be running mainController you should not put it on the ng-controller as well. 1)由于您的基本路由将运行mainController您也不应将其放在ng-controller

2) When you are referencing a module, as in the angular.module('uwApp.controllers') , the module must first have been defined. 2)引用模块时,如在angular.module('uwApp.controllers') ,必须首先定义该模块。 The way to define a module is to have brackets for it's dependencies, like angular.module('uwApp.controllers', []) . 定义模块的方法是对其依赖项使用方括号,例如angular.module('uwApp.controllers', [])

With those things fixed you do not need to do what I said below. 解决了这些问题后,您无需执行我在下面所说的事情。


There is no need to inject the controllers through a separate module. 无需通过单独的模块注入控制器。

You can change pagecontroller.js : 您可以更改pagecontroller.js

angular.module('uwApp.controllers')

to... 至...

angular.module('uwApp')

Then app.js : 然后是app.js

var uwApp = angular.module('uwApp', ['uwApp.controllers','ngRoute'])

to... 至...

var uwApp = angular.module('uwApp', ['ngRoute'])

In pageController.js write like this pageController.js

angular.module('uwApp.controllers', [])
 .controller('mainController', function($scope) {
    $scope.page = 'main controller';
})

some Explanation: 一些解释:

Beware that using angular.module('myModule', []) will create the module myModule and overwrite any existing module named myModule. 请注意,使用angular.module('myModule', [])将创建模块myModule并覆盖任何名为myModule的现有模块。 Use angular.module('myModule') to retrieve an existing module. 使用angular.module('myModule')检索现有模块。

Find more angular docs module 查找更多angular docs模块

There is an interesting style guidelines for angular by jonpapa johnpapa/angular-styleguide jonpapa johnpapa / angular-styleguide提供了有关角的有趣样式指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM