angular框架自身提供的ng-route在一定程度上满足了我们的需求,但是他只针对于单视图,比如点击一个link跳转到另一个视图,但是在实际业务中,需要一个状态对应的视图中还包含其他的视图,或者一个状态对应多个子状态,每个子状态对应一个或多个视图。这时ng-route就不满足了,所以我们需要使用第三方的路由插件ui-router。

1. 引入依赖

<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet">

<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="https://cdn.bootcss.com/angular.js/1.3.7/angular.js"></script>
<script src="https://cdn.bootcss.com/angular-ui-router/1.0.3/angular-ui-router.js"></script>
<script src="https://cdn.bootcss.com/angular-ui-bootstrap/2.5.0/ui-bootstrap.js"></script>
<script src="https://cdn.bootcss.com/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.js"></script>
<script src="js/angular-ui-router-me.js"></script>

2. html页面

<body ng-app="routerApp">

 	<div class="list-group">
<a class="list-group-item" ui-sref="parent0">parent0  <strong>one state --> one view</strong></a>
<a class="list-group-item" ui-sref="parent1">parent1  <strong>one state --> many view</strong></a>
<a class="list-group-item" ui-sref="parent2.c1">parent2.c1  <strong>one state --> child state --> one ciew</strong></a>
<a class="list-group-item" ui-sref="parent2.c2">parent1.c2  <strong>one state --> child state --> many ciew</strong></a>
</div> <div ui-view></div> <script type="text/ng-template" id="parent0">
<div>this is parent0 page{{test}}</div>
</script> <script type="text/ng-template" id="parent1">
<div>this is parent1 page{{test}}</div>
<div ui-view="child1" style="border:1px solid red; width: 400px; height: 300px;"></div>
<div ui-view="child2" style="border:1px solid blue; width: 300px; height: 200px;"></div>
</script> <script type="text/ng-template" id="parent1-c1">
<div>this is parent1 child1 page{{test}}<P>{{pn}}</P></div>
</script> <script type="text/ng-template" id="parent1-c2">
<div>this is parent1 child2 page{{test}}<P>{{pn}}</P></div>
</script> <script type="text/ng-template" id="parent2">
<div>this is parent2 page{{test}}
<div ui-view></div> <!--在含有子状态的视图中,必须要有ui-view来作为子视图的容器--->
</div>
</script> <script type="text/ng-template" id="parent2-c1">
<div>this is parent2-c1 page{{test}}<P>{{pn2}}</P></div>
</script> <script type="text/ng-template" id="parent2-c2">
<div>this is parent2-c2 page{{test}}<P>{{pn2}}</P></div>
<div ui-view="grandson1" style="border:1px solid red; width: 400px; height: 300px;"></div>
<div ui-view="grandson2" style="border:1px solid blue; width: 300px; height: 300px;"></div>
</script> <script type="text/ng-template" id="parent2-c2-g1">
<div>this is parent2-c2-g1 page{{test}}<P>{{pn2}}</P><p>{{pn2cn2}}</p></div>
</script> <script type="text/ng-template" id="parent2-c2-g2">
<div>this is parent2-c2-g2 page{{test}}<P>{{pn2}}</P><p>{{pn2cn2}}</p></div>
</script> </body>

  

 3. 配置路由的js

var uirouterModule = (function(app){

	var myrouter = angular.module("routerApp", ["ui.router", "ui.bootstrap"]);

	myrouter.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {

		$stateProvider
.state('parent0', { //单一视图
url: '/p0',
templateUrl: 'parent0',
controller: 'p0'
})
.state('parent1', { // 一个状态多个视图
url: '/p1',
views: {
'': {
templateUrl: 'parent1',
controller: 'p1'
},
'child1@parent1': {
templateUrl: 'parent1-c1',
controller: 'p1c1'
},
'child2@parent1': {
templateUrl: 'parent1-c2',
controller: 'p1c2'
}
}
})
.state('parent2', {
url: '/p2',
templateUrl: 'parent2',
controller: 'p2',
abstract: true // 提供一个抽象,主要是为了向下提供继承
})
.state('parent2.c1', { //parent2状态下的c1状态
url: '/c1',
templateUrl: 'parent2-c1',
controller: 'p2c1'
})
.state('parent2.c2', { //parent2状态下的c2状态
url: '/c2',
views: {
'': {
templateUrl: 'parent2-c2',
controller: 'p2c2'
},
'grandson1@parent2.c2': {
templateUrl: 'parent2-c2-g1',
controller: 'p2c2g1'
},
'grandson2@parent2.c2': {
templateUrl: 'parent2-c2-g2',
controller: 'p2c2g2'
}
}
})
$urlRouterProvider.otherwise('/p0');
}]); myrouter.controller('p0', function($scope) {
$scope.test = 'p0';
});
myrouter.controller('p1', function($scope) {
$scope.test = 'p1';
$scope.pn ="parentOnly";
});
myrouter.controller('p1c1', function($scope) {
$scope.test = 'p1c1';
});
myrouter.controller('p1c2', function($scope) {
$scope.test = 'p1c2';
}); myrouter.controller('p2', function($scope) {
$scope.test = 'p2';
$scope.pn2 ="parent2Only";
}); myrouter.controller('p2c1', function($scope) {
$scope.test = 'p2c1';
}); myrouter.controller('p2c2', function($scope) {
$scope.test = 'p2c2';
$scope.pn2cn2 = "parent2child2Only";
}); myrouter.controller('p2c2g1', function($scope) {
$scope.test = 'p2c2g1';
});
myrouter.controller('p2c2g2', function($scope) {
$scope.test = 'p2c2g2';
}); app.myrouter = myrouter; return app; }(uirouterModule || {}));

* 路由传参:

传递:

  1. ui-sref="issue.add({deviceId: device.id})";或

   ui-sref="issue.add({device: device});

    2. $state.go('issue.add', {devideId: devideId}); 或

   $state.go('issue.add', {devide: device);

配置:

 .state('issues.add',{
url: '/add/:deviceId', //普通参数直接在url上传递,表现url上
params: {device: null} //// 定义一个空对象,接收数据,同样也可以传递普通参数,但都不会在url上显示
templateUrl: '/mobile/issue/add',
controller: 'IssueAddMobileCtrl'
})

  接收:

$scope.deviceId = $stateParams.deviceId;

  

有不足的地方欢迎随时指正

  

使用angular-ui-router替代ng-router的更多相关文章

  1. vue router.push(),router.replace(),router.go()和router.replace后需要返回两次的问题

    转载:https://www.cnblogs.com/lwwen/p/7245083.html https://blog.csdn.net/qq_15385627/article/details/83 ...

  2. [mobile angular ui 1.2]桌面环境下如何自动隐藏左侧的sidebar?how to hide left sidebar on desktop browser by default?

    使用mobile angular ui 1.2开发,在默认情况下,桌面浏览器中sidebar-left是默认打开的,怎么才能在程序初始打开时关闭sidebar-left呢? 目前我找到的唯一可行办法就 ...

  3. [译]发布ABP v0.19包含Angular UI选项

    发布ABP v0.19包含Angular UI选项 ABP v0.19已发布,包含解决的~90个问题和600+次提交. 新功能 Angular UI 终于,ABP有了一个SPA UI选项,使用最新的A ...

  4. vue 中router.go;router.push和router.replace的区别

    vue 中router.go:router.push和router.replace的区别:https://blog.csdn.net/div_ma/article/details/79467165 t ...

  5. angular ui.router 路由传参数

    angular已经用了一段时间了,最近在做路由,做一下笔记. 路由跳转的时候进行穿参 ui.router方式 <a ui-sref="edit({id:5})"> 编辑 ...

  6. 规范 : angular ui router path & params

    在seo文章中提到url的path 必须是 why-us,而不是whyUS 所以定了规范,所有的path 必须why-us path ?后尾的是用来filter的,所以可以WhyUs 如果是不需要给s ...

  7. [Angular2 Router] Lazy Load Angular 2 Modules with the Router

    Angular 2 lazy loading is a core feature of Angular 2. Lazy loading allows your application to start ...

  8. Angular入门(四) Router 替换当前页面

    1.在 xx.html 中直接 写标签       <a [routerLink]="['/home']">home</a>   2.在 xx.html 中 ...

  9. [Angular 2] Router basic and Router Params

    When we define router in Angualr 2, we use @RouteConcfig() When we want to display component, we use ...

  10. Angular -ui - BootStrap组件的解释以及使用

    关于UI BootStrap UI BootStrap 是angularUI团队用纯粹angularJS语法编写的Bootstrap组件. 1. 关于ng-router(angular-router. ...

随机推荐

  1. python 下划线--完美解释

    Python 用下划线作为变量前缀和后缀指定特殊变量 _xxx 不能用'from module import *'导入 __xxx__ 系统定义名字 __xxx 类中的私有变量名 核心风格:避免用下划 ...

  2. Apache+PHP+Mysql中文配置

    一.安装Apache2 1.输入sudo apt-get install apache2下载安装apache2 2.输入Y回车确认 3.安装成功 Apache安装完成后,默认的网站根目录是" ...

  3. R-CNN算法概要

    参考论文:Rich feature hierarchies for accurate object detection and semantic segmentation 下载地址:https://a ...

  4. 关于 target="_blank"漏洞的分析

    创建: 于 八月 30, 2016 关于 target="_blank"漏洞的分析  一.漏洞详情:首先攻击者能够将链接(指向攻击者自己控制的页面的,该被控页面的js脚本可以对母页 ...

  5. 讯飞语音JavaWeb语音合成解决方案

    在线语音合成 将文字信息转化为声音信息,给应用配上"嘴巴".我们提供了众多极具特色的发音人(音库)供您选择.其合成音在音色.自然度等方面的表现均接近甚至超过了人声.这种语音合成体验 ...

  6. Caffe、TensorFlow、MXnet三个开源库对比

    库名称 开发语言 支持接口 安装难度(ubuntu) 文档风格 示例 支持模型 上手难易 Caffe c++/cuda c++/python/matlab *** * *** CNN ** MXNet ...

  7. R语言 文本挖掘 tm包 使用

    #清除内存空间 rm(list=ls()) #导入tm包 library(tm) library(SnowballC) #查看tm包的文档 #vignette("tm") ##1. ...

  8. Caused by: java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index

    1.错误描述 WARN:2015-05-01 16:45:37[main] - Exception encountered during context initialization - cancel ...

  9. java.sql.SQLException: ORA-01841

    1.错误描述 body = (null) clientId = "DB719904-1E0C-35DC-725D-86ABCF2B6EEC" correlationId = &qu ...

  10. Flex中对表格中某列的值进行数字格式化并求百分比

    1.问题背景 一般的,需要对表格中某列的数值进行格式化,对该数值乘以100,并保留两位小数,添加"%" 2.实现实例 <?xml version="1.0" ...