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

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

问题描述

控制器无法识别-AngularJS错误



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


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


我已经检查了文档和另一个具有相同错误的问题,但没有帮助。



Index.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>
<!-控制器->
< script src = assets / js / controllers / controllerPloyee.js>< / script>
<!-服务->
< script src = services / routeConfig.js>< / script>
< / head>
< body>
< div ng-view>< / div>
< div ng-include =’view / footer.html’>< / div>
< / body>
< / html>

routeConfig.js

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

controllerPloyee.js

  angular.module('ployee')。controller('controllerPloyee',函数($ scope){
});


解决方案

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

 <!-Angular-> 
< script src = lib / angular.min.js>< / script>
< script src = lib / angular-route.js>< / script>
<!-重要事项需要放在这里->
< script src = services / routeConfig.js>< / script>
<!-JS->
< script src = assets / js / moduloPloyee.js>< / script>
<!-控制器->
< script src = assets / js / controllers / controllerPloyee.js>< / script>
<!-服务->
̶<̶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̶ r&c; p
$ bbbgtbgt $ b

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



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



模块是容器为您的应用。



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



从文档中: 1


创建与检索



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


有关更多信息,请参见




Controller is not recognizing - AngularJS error

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 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.

Index.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

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

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̶>̶

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.

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

From the Docs:1

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.

For more information, see

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

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