我正在尝试创建一个单页网站,根据点击引导导航栏上的按钮加载视图。但是,页面的内容不会显示。谁能告诉我出了什么问题?
好的,我有这个index.html:
<!DOCTYPE html>
<!-- define angular app -->
<html ng-app="scotchApp">
<head>
<!-- SCROLLS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" />
<!-- SPELLS -->
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
<script src="script.js"></script>
<script src="controllers/userdetailsController.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="utils/basicUtils.js"></script>
</head>
<!-- define angular controller -->
<body ng-controller="mainController">
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/">Angular Routing Example</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><i class="fa fa-home"></i> User</a></li>
<li><a href="#about"><i class="fa fa-shield"></i> About</a></li>
<li><a href="#contact"><i class="fa fa-comment"></i> Contact</a></li>
</ul>
</div>
</nav>
<div id="main">
<!-- angular templating -->
<!-- this is where content will be injected -->
<div ng-view></div>
</div>
</body>
</html>
这个script.js:
// create the module and name it scotchApp
var scotchApp = angular.module('hyprPortal', ['ngRoute']);
// configure our routes
scotchApp.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'pages/home.html',
controller : 'mainController'
})
// route for the about page
.when('/about', {
templateUrl : 'pages/about.html',
controller : 'aboutController'
})
// route for the contact page
.when('/contact', {
templateUrl : 'pages/contact.html',
controller : 'contactController'
});
});
//create the controller and inject Angular's $scope
scotchApp.controller('mainController', function($scope) {
// create a message to display in our view
$scope.message = 'Everyone come and see how good I look!';
});
scotchApp.controller('aboutController', function($scope) {
$scope.message = 'Look! I am an about page.';
});
scotchApp.controller('contactController', function($scope) {
$scope.message = 'Contact us! JK. This is just a demo.';
});
这个home.html:
<!doctype html>
<html >
<head>
<title>User Details</title>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-route.min.js"></script>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src = "../controllers/userdetailsController.js"></script>
<script src = "../utils/basicUtils.js"></script>
</head>
<body>
<div ng-app="hyprPortal" ng-controller="UserRegCtrl">
<!--<form ng-submit = "add(USERID)">
User ID:<br>
<input type = "text" name = "USERID" ng-model = "USERID"><br>
<input type = "submit" value = "Submit">
</form>-->
<table border="1">
<tr>
<td>{{returnName}}</td>
<td>{{AID}}</td>
</tr>
</table>
</div>
</body>
</html>
答案 0 :(得分:1)
您的代码存在一些问题。
1:home.html不需要拥有所有头部,身体,部位。 2:模块名称应为scothApp,并且应仅作为ng-app =&#34; scothApp&#34;在index.html中 3:你的路线已经告诉Home.html的控制器是mainController所以你不需要使用ng-controller =&#34; UserRegCtrl&#34;添加另一个cotroller。在home.html。
根据您的代码查看此plunker。
https://plnkr.co/edit/Bvo3LkVmGNGZWxF4M66m?p=preview
var scotchApp = angular.module('scotchApp', ['ngRoute']);