如何使用ui-router?

大家好,我是IT修真院武汉分院第13期学员,一枚正直善良的web程序员。

 

今天给大家分享一下:

如何使用ui-router?

 

 

1.配置使用ui-router

1.1导入js文件

需要注意的是:必须导入angular.min.js这个文件,且angular.min.js必须导入在angular-ui-router.min.js前面。

 
  1. <script type="text/javascript" src="JS/angular.min.js"></script>

  2. <script type="text/javascript" src="JS/angular-ui-router.min.js"></script>

  • 1
  • 2

1.2注入angular模块

var app = angular.module('myApp', ['ui.router']);
  • 1

注入的名字“ui.router”,可在angular-ui-router.min.js里找到,如下图: 
这里写图片描述

1.3定义视图

ui-view替代的是ngroute路由的ng-view。

<div ui-view></div>
  • 1

1.4配置路由状态

 
  1. app.config(["$stateProvider", function ($stateProvider){

  2. $stateProvider

  3. .state("home", { //导航用的名字,如<a ui-sref="login">login</a>里的login

  4. url: '/', //访问路径

  5. template:'<div>模板内容......</div>'

  6. })

  7.  
  8. }]);

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.简单示例

 
  1. <html>

  2. <head>

  3. <title>ui-router</title>

  4. <meta http-equiv="pragma" content="no-cache">

  5. <meta http-equiv="cache-control" content="no-cache">

  6. <meta http-equiv="expires" content="0">

  7. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

  8. <meta http-equiv="description" content="This is my page">

  9. <!-- 导入JS -->

  10. <script type="text/javascript" src="JS/angular.min.js"></script>

  11. <script type="text/javascript" src="JS/angular-ui-router.min.js"></script>

  12. </head>

  13.  
  14. <body >

  15. <div ng-app="myApp">

  16. <div ui-view></div> <!-- 视图 -->

  17. </div>

  18. </body>

  19.  
  20.  
  21. <script type="text/javascript">

  22. //定义模板,并注入ui-router

  23. var app = angular.module('myApp', ['ui.router']);

  24. //对服务进行参数初始化,这里配stateProvider服务的视图控制

  25. app.config(["$stateProvider", function ($stateProvider) {

  26. $stateProvider

  27. .state("home", {

  28. url: '/',

  29. template:'<div>模板内容......</div>'

  30. })

  31. }]);

  32. </script>

  33.  
  34. </html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

3.嵌套路由的实现

通过url参数的设置实现路由的嵌套(父路由与子路由通过”.“连接就形成了子路由)。嵌套路由可实现多层次的ui-view。

 
  1. <body >

  2. <div ng-app="myApp" >

  3. <a ui-sref="parent">点我显示父view内容</a>

  4. <a ui-sref="parent.child">点我显示父view与子view内容</a>

  5. <div ui-view></div> <!-- 父View -->

  6. </div>

  7. </body>

  8.  
  9.  
  10. <script type="text/javascript">

  11. var app = angular.module('myApp', ['ui.router']);

  12. app.config(["$stateProvider", function ($stateProvider) {

  13. $stateProvider

  14. .state("parent", {//父路由

  15. url: '/parent',

  16. template:'<div>parent'

  17. +'<div ui-view><div>'// 子View

  18. +'</div>'

  19. })

  20. .state("parent.child", {//子路由

  21. url: '/child',

  22. template:'<div>child</div>'

  23. })

  24. }]);

  25.  
  26. </script>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

上面的是相对路径方式: 
‘parent’将匹配…./index.html#/parent; ‘parent.child’将匹配…./index.html#/parent/child。 
若改成绝对路径方式,则需要在子url里加上^:

 
  1. .state("parent.child", {

  2. url: '^/child',

  3. template:'<div>child</div>'

  4. })

  • 1
  • 2
  • 3
  • 4

此时,’parent’将匹配…./index.html#/parent; ‘parent.child’将匹配…./index.html#/child。

4. 通过views实现多视图

多个示图时,使用views属性。该属性里包含了哪些ui-view,则对应的template或templateUrl里的内容就会填充该ui-view。

同一个状态下有多个视图示例:

 
  1. <body >

  2. <div ng-app="myApp" >

  3. <a ui-sref="index">点我显示index内容</a>

  4. <div ui-view="header"></div>

  5. <div ui-view="nav"></div>

  6. <div ui-view="body"></div>

  7. </div>

  8. </body>

  9.  
  10. <script type="text/javascript">

  11. var app = angular.module('myApp', ['ui.router']);

  12. app.config(["$stateProvider", function ($stateProvider) {

  13. $stateProvider

  14. .state("index", {

  15. url: '/index',

  16. views:{

  17. 'header':{template:"<div>头部内容</div>"},

  18. 'nav':{template:"<div>菜单内容</div>"},

  19. 'body':{template:"<div>展示内容</div>"}

  20. }

  21. })

  22.  
  23. }]);

  24.  
  25. </script>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

5.ui-view的定位

@的作用 是用来绝对定位view,即说明该ui-view属于哪个模板。如:’header@index’表示名为header的view属于index模板。绝对和相对路径的效果一样,请看如下代码:

 
  1. <body >

  2. <div ng-app="myApp" >

  3. <a ui-sref="index">show index</a>

  4. <a ui-sref="index.content1">content111111</a>

  5. <a ui-sref="index.content2">content222222</a>

  6. <div ui-view="index"><div>

  7. </div>

  8. </body>

  9.  
  10. <script type="text/javascript">

  11. var app = angular.module('myApp', ['ui.router']);

  12. app.config(["$stateProvider", function ($stateProvider) {

  13. $stateProvider

  14. .state("index", {

  15. url: '/index',

  16. views:{

  17. 'index':{template:"<div><div ui-view='header'></div> <div ui-view='nav'></div> <div ui-view='body'></div> </div>"},

  18. //这里必须要绝对定位

  19. 'header@index':{template:"<div>头部内容header</div>"},

  20. 'nav@index':{template:"<div>菜单内容nav</div>"},

  21. 'body@index':{template:"<div>展示内容contents</div>"}

  22. }

  23. })

  24. //绝对定位

  25. .state("index.content1", {

  26. url: '/content1',

  27. views:{

  28. 'body@index':{template:"<div>content11111111111111111</div>"}

  29. //'body@index'表时名为body的view使用index模板

  30. }

  31. })

  32. //相对定位:该状态的里的名为body的ui-view为相对路径下的(即没有说明具体是哪个模板下的)

  33. .state("index.content2", {

  34. url: '/content2',

  35. views:{

  36. 'body':{template:"<div>content2222222222222222222</div>"}//

  37. }

  38. })

  39.  
  40. }]);

  41.  
  42. </script>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

由上面代码可知,相对定位不能找到的ui-view需要用@来绝对定位。

 

三个问题:

 

 1、ui-sref 和 $state.go 的区别

ui-sref 一般使用在 <a> 标签上;而$state.go一般使用在 controller 里面。

2、$stateParams怎样获取参数?

在目标页面的controller里注入$stateParams,然后 "$stateParams.参数名" 获取传递的参数。

3、UI-ROUTER如何设置?

这里是如何设置一个基本url。

$stateProvider

.state('contacts', {

url:"/contacts",

templateUrl:'contacts.html'

    })

当我们访问index.html/contacts时,'contacts'状态将被激活,同时index.html中的ui-view将被'contacts.html'填充。或者,通过transitionTo('contacts')方法将状态转变到'contacts'状态,同时 url 将更新为index.html/contacts。


 

今天的分享就到这里啦,欢迎大家点赞、转发、留言、拍砖~

技能树-IT修真院

IT修真院是一个免费的线上IT技术学习平台 。

每个职业以15个左右的task为初学者提供更快速高效的学习方式 ;

所有task均是从真实项目中提炼出来的技能点,

强调实战演练+自学优先+师兄辅导的学习方式,

严格的日报体系,欢乐的交流讨论学习气氛,更有无数师兄师姐帮你解疑

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值