AngularJS实战之路由ui-view

时间:2023-12-21 20:13:20

1. 路由(ui-router)

1.1. 环境

1) angular.min.js

2) angular-ui-router-0.2.10.js

3) 确保确保包含ui.router为模块依赖关系.

4) var app=angular.module("app",['ui.router']);

1.1. 视图激活方式

1) $state.Go():优先级较高的便利方式

2) ui-sref:点击包含此指令跳转

3) url:url导航

1.2. 多视图

Views:

使用views属性设置多个视图。如果在单一状态下不需要多个视图,则不需要此属性。提示:请记住,通常嵌套视图比多个兄弟视图更有用和更强大。

代码:准备四个页面  主页面和三个子页面

主页面

<!DOCTYPE html>
<html ng-app="app">
<head>
<title>多视图</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="plugins/bootstrap/css/bootstrap.min.css"
type="text/css"></link>
</head>
<body>
<div class="container">
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">柳絮飞祭奠</a>
</div>
<div>
<ul class="nav navbar-nav">
<li><a ui-sref="index2">onepage</a></li>
<li><a ui-sref="index1">twopage</a></li>
</ul>
</div>
</div>
</nav>
</div>
<div ui-view></div>
<div ui-view="index1"></div>
<div ui-view="index2"></div>
</body>
<script type="text/javascript"
src="plugins/angular/angular.min-1.4.6.js"></script>
<script type="text/javascript" src="plugins/angular/angular-ui-router-0.2.10.js"></script>
<script type="text/javascript">
var app=angular.module("app",['ui.router']);
app.config(['$stateProvider','$urlRouterProvider',function($stateProvider,$urlRouterProvider){
$stateProvider.state('index1', {
url : '/index1',
views:{
"index1":{
templateUrl: '/CeShi/lx/uiroute/index1.html'
},
"index2":{
templateUrl : '/CeShi/lx/uiroute/index2.html'
}
}
}).state('index2', {
url : '/index2',
templateUrl: '/CeShi/lx/uiroute/index3.html'
});
$urlRouterProvider.otherwise('/index2');
}]).config(function($sceProvider){
$sceProvider.enabled(false);
});
</script>
</html>

三个子页面index1.html,index2.html,index3.html分别为

<h1>我是视图一</h1>

<h1>我是视图二</h1>

<h1>我是视图三</h1>

效果为

AngularJS实战之路由ui-view

AngularJS实战之路由ui-view

1.3.嵌套视图

依旧四个页面一个主页面一个子页面两个嵌套页面



主页面

<!DOCTYPE html>
<html ng-app="app">
<head>
<title>嵌套视图</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../plugins/bootstrap/css/bootstrap.min.css" type="text/css"></link>
</head>
<body>
<div class="container">
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">柳絮飞祭奠</a>
</div>
</div>
</nav>
</div>
<div ui-view></div>
</body>
<script type="text/javascript" src="../../plugins/angular/angular.min-1.4.6.js"></script>
<script type="text/javascript" src="../../plugins/angular/angular-ui-router-0.2.10.js"></script>
<script type="text/javascript">
var app=angular.module("app",['ui.router']);
app.config(['$stateProvider','$urlRouterProvider',function($stateProvider,$urlRouterProvider){
$stateProvider.state('index2', {
url : '/index2',
templateUrl : '/Angular/uiview/nested/index2.html'
}).state('index2.one',{
url:'/nested',
templateUrl: '/Angular/uiview/nested/index1.html'
}).state('index2.two',{
url:'/nested',
templateUrl: '/Angular/uiview/nested/index3.html'
});
$urlRouterProvider.otherwise('/index2');
}]).config(function($sceProvider){
$sceProvider.enabled(false);
});
</script>
</html>

子页面

<div class="container">
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">柳絮飞祭奠</a>
</div>
<div>
<ul class="nav navbar-nav">
<li class="active"><a ui-sref="index2.one">onepage</a></li>
<li><a ui-sref="index2.two">twopage</a></li>
</ul>
</div>
</div>
</nav>
</div>
<div ui-view></div>

嵌套页面

<h1>我是视图一</h1>

<h1>我是视图三</h1>

效果:



AngularJS实战之路由ui-view