angularjs - AngularJS:ng-view 指令不起作用
问题描述
我现在正在学习 AngularJS,我正在做一个简单的 Web 应用程序,其中涉及使用 ng-view 指令。但是当我尝试运行我的代码时,我的 Chrome 浏览器上什么也没有出现,好像 ng-view 没有真正工作。我的 index.html 文件如下所示:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Route Sample</title>
<script src="https://code.angularjs.org/1.6.9/angular-route.js"></script>
<script src="https://code.angularjs.org/1.6.9/angular.min.js"></script>
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
</head>
<body ng-app="routeApp">
<h1>Sample Routing | ng-view directive</h1>
<div class="container">
<ul>
<li><a href="#Angular">Angular JS Topics</a></li>
<li><a href="#Node">Node JS Topics</a></li>
</ul>
</div>
<div class="container-ngView">
<div ng-view></div>
</div>
<script>
var app=angular.module('routeApp', ['ngRoute']);
app.config(['$routeProvider', function($routeProvider){
$routeProvider
.when('/Angular', {
templateUrl: 'angular.html',
controller: 'AngularCtrl',
})
.when('/Node', {
templateUrl: 'node.html',
controller: 'NodeCtrl',
});
}]);
app.controller('AngularCtrl', function($scope){
$scope.tutorial = [
{Name:"Controllers", Description :"Controllers in action"},
{Name:"Models", Description :"Models and binding data"},
{Name:"Directives", Description :"Flexibility of Directives"}
]
});
app.controller('NodeCtrl', function($scope){
$scope.tutorial = [
{Name:"Promises", Description :"Power of Promises"},
{Name:"Event", Description :"Event of Node.js"},
{Name:"Modules", Description :"Modules in Node.js"}
]
});
</script>
</body>
</html>
这是我的 angular.html 文件
<h2>Anguler</h2>
<ul ng-repeat="ptutor in tutorial">
<li>Course : {{ptutor.Name}} - {{ptutor.Description}}</li>
</ul>
这是我的 node.html 文件
<h2>Node</h2>
<ul ng-repeat="ptutor in tutorial">
<li>Course : {{ptutor.Name}} - {{ptutor.Description}}</li>
</ul>
我确实尝试将我的 js 脚本文件与我的主 html 文件分开,但结果仍然相同。我不知道该怎么做以及在哪里找到问题。我希望你能帮我解决这个问题。谢谢!
解决方案
您需要在 AngularJS之后引用 ngRoute 模块脚本。简单地交换它们。
此外,您使用的是 AngularJS 1.6.X,它在 URL 中有不同的 hash-bang。您需要将您的href
from更改#
为#!
.
这是一个工作示例:
var app = angular.module('routeApp', ['ngRoute']);
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/Angular', {
templateUrl: 'angular.html',
controller: 'AngularCtrl',
})
.when('/Node', {
templateUrl: 'node.html',
controller: 'NodeCtrl',
});
}]);
app.controller('AngularCtrl', function($scope) {
$scope.tutorial = [{
Name: "Controllers",
Description: "Controllers in action"
},
{
Name: "Models",
Description: "Models and binding data"
},
{
Name: "Directives",
Description: "Flexibility of Directives"
}
]
});
app.controller('NodeCtrl', function($scope) {
$scope.tutorial = [{
Name: "Promises",
Description: "Power of Promises"
},
{
Name: "Event",
Description: "Event of Node.js"
},
{
Name: "Modules",
Description: "Modules in Node.js"
}
]
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Route Sample</title>
<script src="https://code.angularjs.org/1.6.9/angular.min.js"></script>
<script src="https://code.angularjs.org/1.6.9/angular-route.js"></script>
</head>
<body ng-app="routeApp">
<h1>Sample Routing | ng-view directive</h1>
<div class="container">
<ul>
<li><a href="#!Angular">Angular JS Topics</a></li>
<li><a href="#!Node">Node JS Topics</a></li>
</ul>
</div>
<div class="container-ngView">
<div ng-view></div>
</div>
<script type="text/ng-template" id="angular.html">
<h2>Angular</h2>
<ul ng-repeat="ptutor in tutorial">
<li>Course : {{ptutor.Name}} - {{ptutor.Description}}</li>
</ul>
<a href="#!/">Back</a>
</script>
<script type="text/ng-template" id="node.html">
<h2>Node</h2>
<ul ng-repeat="ptutor in tutorial">
<li>Course : {{ptutor.Name}} - {{ptutor.Description}}</li>
</ul>
<a href="#!/">Back</a>
</script>
</body>
</html>
推荐阅读
- sql - INSERT SELECT FOR UPDATE ON CONFLICT DO NOTING RETURNING 的语义
- spring - spring mvc登录表单验证
- javascript - 如何在不使用 scaleTime 的情况下使用 d3 v4 创建多折线图?
- c# - 如何使用 Await 方法加载新页面
- javascript - .#datepicker 不工作是错误和图标没有被点击?
- jboss - jboss中的套接字超时为7.1.1
- github - GitHub 是否关闭了对 Subversion 客户端的支持?
- javascript - 为什么这两个表达式会产生不同的值?
- reactjs - 如何更改属性溢出?
- rest - REST 验证由身份验证服务生成的业务 API 中的令牌