错误:$ controller:ctrlreg未注册名为“{0}”的控制器 [英] Error: $controller:ctrlreg The controller with the name '{0}' is not registered
问题描述
app.js
(function(){
'use strict';
angular
.module('app', ['ngRoute', 'ngCookies'])
.config(config)
config.$inject = ['$routeProvider', '$locationProvider'];
function config($routeProvider, $locationProvider){
$routeProvider
.when('/', {
controller: 'HomeController',
templateUrl: 'home/home.html',
controllerAs: 'vm'
})
}
})();
home.controller.js
(function () {
'use strict';
angular
.module('app')
.controller('HomeController', HomeController);
HomeController.$inject = ['UserService', '$rootScope'];
function HomeController(UserService, $rootScope) {
$rootScope.bodylayout ='main_page_que';
var vm = this;
}
})();
home.js
var app = angular.module('app', []);
app.controller('RedCtrl', function($scope) {
$scope.OpenRed = function() {
$scope.userRed = !$scope.userRed;
$scope.userBlue = false;
}
$scope.HideRed = function() {
$scope.userRed = false;
}
$scope.OpenBlue = function() {
$scope.userBlue = !$scope.userBlue;
$scope.userRed = false;
};
$scope.HideBlue = function() {
$scope.userBlue = false;
};
});
home.html
<div ng-controller="RedCtrl">
<a href="#" ng-click="OpenRed()" hide-red="HideRed()">Show Red</a>
<div hide-red="HideRed()" class="loginBox" ng-show="userRed"></div>
<a href="#" ng-click="OpenBlue()" hide-blue="HideBlue()">Show Blue</a>
<div hide-blue="HideBlue()" class="loginBoxBlue" ng-show="userBlue"></div>
</div>
Index.html
<html lang="pl" ng-app="app">
<body class="{{bodylayout}}">
<div class="content">
<div ng-view style="margin:auto"></div>
</div>
<script src="//code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="//code.angularjs.org/1.6.0/angular.min.js"></script>
<script src="//code.angularjs.org/1.6.0/angular-route.min.js"></script>
<script src="//code.angularjs.org/1.6.0/angular-cookies.min.js"></script>
<script src="home.js"></script>
<script src="app.js"></script>
<script src="authentication.service.js"></script>
<script src="flash.service.js"></script>
<script src="user.service.local-storage.js"></script>
<script src="home/home.controller.js"></script>
</body>
</html>
嘿,当我使用ngRoute并动态添加 home时,这是应用程序的一部分.html
,但是当我添加外部文件 home.js
时,我认为我有错误的app.js或home.controller.js(当时是 ng-controller =RedCtrl
。我收到错误 [$ controller:ctrlreg] RedCtrl没有注册
。你有没有想法为什么?我是Angular的新人,我认为主要原因是我缺乏知识。
Hey, this is part from application when I use ngRoute and dynamically add home.html
, but I think I have wrong app.js or home.controller.js beacuse when I add external file home.js
(when is ng-controller=""RedCtrl
. I get error [$controller:ctrlreg] RedCtrl is not register
. Have you ideas why? I am new in Angular and I think the main reason is lack of my knowledge.
推荐答案
你正在覆盖<$ home.js 中的c $ c> app 模块因此收到错误
You are overwriting app
module in home.js thus getting the error
使用
var app = angular.module('app'); //Get previously defined module
而不是
var app = angular.module('app', []); //It declares new module
您还需要更改JavaScript文件的顺序
You also need to change the sequence of JavaScript files
<script src="app.js"></script>
<script src="home.js"></script>
这篇关于错误:$ controller:ctrlreg未注册名为“{0}”的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!