我的索引文件:
<!DOCTYPE html>
<html ng-app="myApp" ng-controller="homingController">
<head>
<title></title>
<script src="./js/jquery-1.9.1.js"></script>
<script src="./js/jquery-ui.js"></script>
<script src="./js/angular.js"></script>
<script src="./js/angular-ui-router.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular-route.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.min.js"></script>
<script src="./Controller/homingController.js"></script>
<link rel = "stylesheet" href = "./css/angular-material.min.css">
<link rel="stylesheet" href="./css/bootstrap-theme.min.css">
<link rel = "stylesheet" href = "./css/materialize.min.css">
<link href="./css/icon.css" rel="stylesheet">
<link rel ="stylesheet" href="./css/Main.css">
<title>Home Application</title>
</head>
<body>
<div class="navbar">
<a href="#/home">Home</a>
<a href="#/monitor">about</a>
</div>
<div ng-view></div>
<script>
var mainApp = angular.module('myApp', ['ngRoute']);
mainApp.config(function($routeProvider) {
$routeProvider.when('/home', {
template: "home",
controller: 'homingController'
})
.when('/monitor', {
template: "<p>{{firstName}}{{lastName}}</p>",
controller: 'monitoringController'
})
.
otherwise({
redirectTo: '/'
});
});
mainApp.controller('monitoringController', ['$scope', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
}])
我的 HomingController.js
var mainApp = angular.module('myApp', ['ngRoute']);
mainApp.controller('homingController', ['$scope', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
}])
我的Home.html代码:
<div ng-controller="homingController">
{{ lastName }} Hello
</div>
现在我面临问题:
1)在 homingController 中,如果我添加第一行:
var mainApp = angular.module('myApp', ['ngRoute']);
然后它给我错误:
angular.js:14328 错误:[$controller:ctrlreg] 名为“homingController”的 Controller 未注册。
1)在 homingController 中,如果我不添加,则添加第一行:
var mainApp = angular.module('myApp', ['ngRoute']);
然后它给我错误:
angular.js:14328 错误:[$controller:ctrlreg] 名为“homingController”的 Controller 未注册。
谢谢!!
最佳答案
您声明了该模块两次,
//删除 controller.js
文件中的这一行
var app = angular.module("myApp", []);
///
app.controller("homingController", function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
<强> DEMO
关于javascript - 为什么我的 Controller 没有在下面的代码中注册?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46804850/