添加ngRoute`.config`后控制器未注册错误 [英] Controller is not registered error after adding ngRoute `.config`

查看:19
本文介绍了添加ngRoute`.config`后控制器未注册错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 controllerPloyee.js 中定义了一个名为 controllerPloyee 的控制器,它在我添加 ngRoute 并进行路由之前工作.显示的错误是

I've a controller defined in controllerPloyee.js called controllerPloyee which was working before i added the ngRoute and made to route. The error that shows is

angular.min.js:127 错误:[$controller:ctrlreg] http://errors.angularjs.org/1.7.8/$controller/ctrlreg?p0=controllerPloyee

angular.min.js:127 Error: [$controller:ctrlreg] http://errors.angularjs.org/1.7.8/$controller/ctrlreg?p0=controllerPloyee

我已经检查了文档和其他有相同错误的问题,但没有帮助.

I've checked the documentation and another questions with the same error, but doesn't help.

索引.html

<html ng-app="ployee">
<head>
    <!--Angular-->
    <script src="lib/angular.min.js"></script>
    <script src="lib/angular-route.js"></script>
    <!--JS-->
    <script src="assets/js/moduloPloyee.js"></script>
    <!--Controllers-->
    <script src="assets/js/controllers/controllerPloyee.js"></script>
    <!--Services-->
    <script src="services/routeConfig.js"></script>
</head>
<body>
    <div ng-view></div>
    <div ng-include="'view/footer.html'"></div>
</body>
</html>

routeConfig.js

routeConfig.js

angular.module('ployee', ['ngRoute']).config(function($routeProvider){
    $routeProvider.when('/login', {
        templateUrl: "view/login.html",
        controller: "controllerPloyee"
    }).when('/',{
        templateUrl: "view/login.html",
        controller: "controllerPloyee"
    }).otherwise({redirectTo: "/login"})
})

controllerPloyee.js

controllerPloyee.js

angular.module('ployee').controller('controllerPloyee',  function($scope){
});

推荐答案

创建模块的脚本需要放在其他脚本之前:

The script that creates the module needs to be placed before the others:

<!--Angular-->
<script src="lib/angular.min.js"></script>
<script src="lib/angular-route.js"></script>
<!-- IMPORTANT needs to be place here -->
<script src="services/routeConfig.js"></script>
<!--JS-->
<script src="assets/js/moduloPloyee.js"></script>
<!--Controllers-->
<script src="assets/js/controllers/controllerPloyee.js"></script>
<!--Services-->
̶<̶s̶c̶r̶i̶p̶t̶ ̶s̶r̶c̶=̶"̶s̶e̶r̶v̶i̶c̶e̶s̶/̶r̶o̶u̶t̶e̶C̶o̶n̶f̶i̶g̶.̶j̶s̶"̶>̶<̶/̶s̶c̶r̶i̶p̶t̶>̶

语句 angular.module('ployee', ['ngRoute']) 创建一个新模块.语句 angular.module('ployee') 检索模块以进行进一步配置.

The statement angular.module('ployee', ['ngRoute']) creates a new module. The statement angular.module('ployee') retrieves the module for further configuration.

当模块在控制器脚本之后创建时,控制器丢失.这就是为什么创建模块的脚本需要放在其他添加控制器、服务、过滤器、指令、常量等的脚本之前.

When the module is created after the controller script, the controller is lost. This is why the script that creates the module needs to be placed before other scripts which add controllers, services, filters, directives, constants, etc.

模块是应用程序的容器.

A module is a container for your app.

传递一个参数会检索现有的 angular.Module,而传递多个参数会创建一个新的 angular.Module.

Passing one argument retrieves an existing angular.Module, whereas passing more than one argument creates a new angular.Module.

来自文档:1

From the Docs:1

请注意,使用 angular.module('myModule', []) 将创建模块 myModule覆盖任何名为 myModule<的现有模块/code>. 使用 angular.module('myModule') 检索现有模块.

Creation versus Retrieval

Beware that using angular.module('myModule', []) will create the module myModule and overwrite any existing module named myModule. Use angular.module('myModule') to retrieve an existing module.

有关详细信息,请参阅

  • AngularJS angular.module Function API Reference
  • AngularJS angular.Module Type API Reference
  • AngularJS Developer Guide - modules

这篇关于添加ngRoute`.config`后控制器未注册错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆