最近研究了一下ui-view多层嵌套,整理了一下

1.最简单的ui-view用法

html部分:

<ul class="nav navbar-nav">
<li ui-sref-active="active"><a ui-sref="home">首页</a></li>
<li ui-sref-active="active"><a ui-sref="news">新闻</a></li>
<li ui-sref-active="active"><a ui-sref="center">个人中心</a></li>
</ul>
<div class="wrap" ui-view></div>

路由配置:

var app = angular.module('myApp',["ui.router"]);
app.config(["$stateProvider","$urlRouterProvider", function ($stateProvider,$urlRouterProvider) {
$stateProvider
.state("home", {
url: '/home',
templateUrl:'template/home.html',
controller:'homeCtrl'
})
.state("news", {
url: '/news',
templateUrl:'template/news.html',
controller:'newsCtrl'
})
.state("details",{
url:'/news/details',
templateUrl:'template/newsDetails.html',
controller:'detailsCtrl'
})
.state("center", {
url: '/center',
templateUrl:'template/setCenter.html',
controller:'centerCtrl'
});
$urlRouterProvider.otherwise('/home');
}]);

2.多视口

html部分:

<ul class="nav navbar-nav">
<li ui-sref-active="active"><a ui-sref="home">首页</a></li>
<li ui-sref-active="active"><a ui-sref="home.news">新闻</a></li>
<li ui-sref-active="active"><a ui-sref="home.center">个人中心</a></li>
</ul>
<div class="warp">
<div class="main" ui-view="home"></div>
<div class="left" ui-view="left"></div>
<div class="right" ui-view="right"></div>
</div>

路由配置:

var app = angular.module('myApp',["ui.router","ui.bootstrap"]);
app.config(["$stateProvider","$urlRouterProvider", function ($stateProvider,$urlRouterProvider) {
$stateProvider
.state("home", {
url: '/home',
views:{
'home':{template:"<div class='body' ui-view='body'></div><div class='footer' ui-view='footer'></div>"},
'left':{template:"<span>left</span>"},
'right':{template:"<span>right</span>"},
'body@home':{templateUrl:'template_views/home.html'},
'footer@home':{template:"<span>首页底部</span>"}
}
})
.state('home.news',{
url:'/news',
views:{
'body@home':{templateUrl:'template_views/news.html'},
'footer@home':{template:"<span>新闻底部</span>"}
}
})
.state('home.newsDetails',{
url:"/news/details/:id",
views:{
'body@home':{
template:"<span>新闻详情</span>",
controller:function ($stateParams) {
alert('id:'+ $stateParams.id);
}
},
'footer@home':{template:"<span>新闻详情底部</span>"}
}
})
.state('home.center',{
url:'/center',
views:{
'body@home':{templateUrl:'template_views/setCenter.html'},
'footer@home':{template:"<span>个人中心底部</span>"}
} }); $urlRouterProvider.otherwise('/home');
}]);

这里需要注意的是路由传参,这里把news.html内容贴出来

<uib-tabset active="activeJustified" justified="true">
<uib-tab index="0" heading="推荐">
<div style="width: 100%;height: 500px;background-color:pink;">
<span class="mouseHand btn" ui-sref="home.newsDetails({id:1})">Justified content</span>
</div>
</uib-tab>
<uib-tab index="1" heading="热点">
<div style="width: 100%;height: 500px;background-color:#b9def0;" ui-sref="home.newsDetails({id:2})">Short Labeled Justified content</div>
</uib-tab>
<uib-tab index="2" heading="股票">
<div style="width: 100%;height: 500px;background-color:darkcyan;" ui-sref="home.newsDetails({id:3})">Long Labeled Justified content</div>
</uib-tab>
</uib-tabset>
ui-sref="home.newsDetails({id:1})"这样跳转的时候就把id传给了新闻详情;在新闻详情的控制器里就可以通过$stateParams.id得到传过来的id

3.多层ui-view嵌套
写了一个小demo,首先看一下结构

myExercise.html代码:
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<script src="lib/angular.js"></script>
<script src="lib/angular-ui-router.js"></script>
<script src="lib/jquery-1.12.3.js"></script>
<script src="lib/bootstrap.js"></script>
<script src="lib/ocLazyLoad-master/dist/ocLazyLoad.js"></script>
<script src="js/myConfig.js"></script>
<script src="js/contentCtrl.js"></script>
<link rel="stylesheet" href="lib/bootstrap.css">
<link rel="stylesheet" href="css/base.css">
<title>ui-view三层嵌套</title>
</head>
<body>
<div ui-view class="wrap"></div>
</body>
</html>
ocLazyLoad.js是用来分步加载js,css等文件的,后面可以再路由配置里看到具体用法,有了这个对于项目初次加载的速度有很大提升,但是不需要的这里也可以不必引入;

js文件夹结构

前两个控制器都是空的,没写什么逻辑就不展示了,先看一下myConfig.js,路由配置(多层嵌套精髓都在这里啦):
var app = angular.module('myApp',["ui.router","oc.lazyLoad"]);
app.config(function ($stateProvider,$locationProvider, $urlRouterProvider,$ocLazyLoadProvider) {
$stateProvider
.state('content',{
url: '/',
views:{
"":{
template: '<div ui-view="header"></div><div ui-view="body"></div>',
controller:'contentCtrl'
},
"header@content":{templateUrl: 'myTemplate/header.html'}
}
})
.state('content.home',{
url:'home',
views:{
"body@content":{
templateUrl:'myTemplate/home.html',
controller:'homeCtrl'
}
},
resolve:{
deps: ['$ocLazyLoad', function($ocLazyLoad) {
// you can lazy load files for an existing module
return $ocLazyLoad.load([
'js/homeCtrl.js',
'css/home.css'
]);
}]
}
})
.state('content.news',{
url:'news',
views:{
"body@content":{template:'<div ui-view></div>'}
}
})
.state('content.news.list',{
url:'/list',
templateUrl:'myTemplate/news.html'
})
.state('content.news.list.recommend',{
url:'/recommend',
templateUrl:'myTemplate/news-list-recommend.html'
})
.state('content.news.list.hot',{
url:'/hot',
templateUrl:'myTemplate/news-list-hot.html'
})
.state('content.news.list.sport',{
url:'/sport',
templateUrl:'myTemplate/news-list-sport.html'
})
.state('content.news.details',{
url:'/details/:type/:id',
templateUrl:'myTemplate/news-details.html',
controller:'newsDetailCtrl',
resolve:{
deps: ['$ocLazyLoad', function($ocLazyLoad) {
// you can lazy load files for an existing module
return $ocLazyLoad.load([
'js/newsDetailCtrl.js',
'css/newsDetail.css'
]);
}]
}
})
.state('content.news.details.comment',{
url:'/details/comment',
templateUrl:'myTemplate/news-detail-comment.html'
})
.state('content.about',{
url:'about',
views:{
"body@content":{templateUrl:'myTemplate/about.html'}
}
}); $urlRouterProvider.otherwise('home'); });

结合路由配置,看一下template模板代码:

首先是header.html

<ul class="nav nav-tabs">
<li role="presentation" class="active" ui-sref-active="active"><a ui-sref="content.home">Home</a></li>
<li role="presentation" ui-sref-active="active"><a ui-sref="content.news.list.recommend">News</a></li>
<li role="presentation" ui-sref-active="active"><a ui-sref="content.about">About</a></li>
</ul>

news.html

<ul class="nav nav-tabs">
<li role="presentation" class="active" ui-sref-active="active"><a ui-sref="content.news.list.recommend">推荐</a></li>
<li role="presentation" ui-sref-active="active"><a ui-sref="content.news.list.hot">热门</a></li>
<li role="presentation" ui-sref-active="active"><a ui-sref="content.news.list.sport">体育</a></li>
</ul>
<div ui-view></div>

news-details.html

<span>{{type}}:新闻详情</span>
<a ui-sref=".comment">查看评论</a>
<div ui-view></div>
<!--news-list-recommend.html-->
<div>
推荐列表
<a ui-sref="^.^.details({type:'recommend',id:2})">点击查看新闻详情,通过相对路径跳转</a>
</div> <!--news-list-hot.html-->
<div>
热门列表
<a ui-sref="content.news.details({type:'hot',id:5})">点击查看新闻详情,通过绝对路径跳转</a>
</div> <!--news-list-sport.html-->
<div>
体育列表
<a ui-sref="content.news.details({type:'sport',id:7})">点击查看新闻详情</a>
</div>

模板就展示这几个了,其它的都是随便写的text文字了,随便写点东西就行,到这里就差不多完成了.

接下来展示一下newsDeatilCtrl.js

angular.module('myApp').controller('newsDetailCtrl',['$scope','$stateParams',function ($scope,$stateParams) {
$scope.type = $stateParams.type;
}]);

这里通过$stateParams来接收路由跳转传过来的参数。

好啦,到这里就大功告成了。


angularjs ui-view多视口多层嵌套路由配置的更多相关文章

  1. 使用ui-route实现多层嵌套路由

    一.预期实现效果: https://liyuan-meng.github.io/uiRouter-app/index.html (项目地址:https://github.com/liyuan-meng ...

  2. VUE router-view 页面布局 (嵌套路由+命名视图)

    嵌套路由 实际生活中的应用界面,通常由多层嵌套的组件组合而成.同样地,URL 中各段动态路径也按某种结构对应嵌套的各层组件,例如: /user/foo/profile /user/foo/posts ...

  3. vue嵌套路由总结

    嵌套路由就是在一个被路由过来的页面下可以继续使用路由,嵌套也就是路由中的路由的意思. 比如在vue中,我们如果不使用嵌套路由,那么只有一个<router-view>,但是如果使用,那么在一 ...

  4. vue路由-动态路由和嵌套路由

    一.动态路由 我们经常需要把某种模式匹配到的所有路由,全都映射到同个组件.例如,我们有一个 User 组件,对于所有 ID 各不相同的用户,都要使用这个组件来渲染.那么,我们可以在 vue-route ...

  5. vue嵌套路由 && 404重定向

    第一部分: vue嵌套路由 嵌套路由是什么? 嵌套路由就是在一个被路由过来的页面下可以继续使用路由,嵌套也就是路由中的路由的意思.  比如在vue中,我们如果不使用嵌套路由,那么只有一个<rou ...

  6. vue嵌套路由与404重定向实现方法分析

    第一部分: vue嵌套路由 嵌套路由是什么? 嵌套路由就是在一个被路由过来的页面下可以继续使用路由,嵌套也就是路由中的路由的意思. 比如在vue中,我们如果不使用嵌套路由,那么只有一个<rout ...

  7. 07. vue-router嵌套路由

    嵌套路由用法 1.嵌套路由功能分析 点击父级路由链接显示模板内容 模板内容中又有子级路由链接 点击子级路由链接显示子级模板内容 2.父路由组件模板 父级路由链接 父组件路由填充位 <p> ...

  8. AngularJS 的嵌套路由 UI-Router

    AngularJS 的嵌套路由 UI-Router 本篇文章翻译自:https://scotch.io/tutorials/angular-routing-using-ui-router 演示网站请查 ...

  9. AngularJS ui-router (嵌套路由)

    http://www.oschina.net/translate/angularjs-ui-router-nested-routes AngularJS ui-router (嵌套路由) 英文原文:A ...

随机推荐

  1. 消息队列 MQ 入门理解

    功能特性: 应用场景: 消息队列 MQ 可应用于如下几个场景: 分布式事务 在传统的事务处理中,多个系统之间的交互耦合到一个事务中,响应时间长,影响系统可用性.引入分布式事务消息,交易系统和消息队列之 ...

  2. 使用Pylint规范你的Python代码

    Pylint是一个Python代码风格的检查工具,功能上类似于pychecker,默认用PEP8作为代码风格标准,它所提供的功能包括:检查代码行的长度,检查变量命名是否符合规范,检查声明的接口是否被真 ...

  3. html基础+常用标签

    概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏览器 ...

  4. 解决后台json数据返回的字段需要替换的问题

    有时候后台json数据返回的字段含有“id”,也有可能是有时候为了减少代码的冗余,两页面之间只是数据模型个别属性的区别,所以这时候最好是用到模型属性的替换,用新的属性替换返回的json数据的字段.这里 ...

  5. Label Propagation Algorithm LPA 标签传播算法解析及matlab代码实现

    转载请注明出处:http://www.cnblogs.com/bethansy/p/6953625.html LPA算法的思路: 首先每个节点有一个自己特有的标签,节点会选择自己邻居中出现次数最多的标 ...

  6. 数据库MySQL(课下作业,必做)

    数据库MySQL(课下作业,必做) 题目要求: 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入 ...

  7. 使用json-lib-*.jar的JSON解析工具类

    使用json-lib-2.4-jdk15.jar JSON工具类: import java.util.List; import net.sf.json.JSONArray; import net.sf ...

  8. nodejs(三) --- nodejs进程与子进程

    嗯,对于node的学习还远远不够,这里先做一个简单的api的记录,后续深入学习. 第一部分:nodejs中的全局对象之process进程对象 在node中的全局对象是global,相当于浏览器中的wi ...

  9. Java之IO(七)ObjectInputStream和ObjectOutputStream

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7003536.html 1.前言 本章介绍Java字节流中重要的成员,对象流ObjectInputStream和O ...

  10. Go语言学习笔记十二: 范围(Range)

    Go语言学习笔记十二: 范围(Range) rang这个关键字主要用来遍历数组,切片,通道或Map.在数组和切片中返回索引值,在Map中返回key. 这个特别像python的方式.不过写法上比较怪异使 ...