script>script type="text/javascrip_angular-ui-router">
当前位置:   article > 正文

AngularJS——ui-router详解_angular-ui-router

angular-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

1.2注入angular模块

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

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

1.3定义视图

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

<div ui-view></div>
 
 
  • 1
  • 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. }]);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 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. <body >
  14. <div ng-app="myApp">
  15. <div ui-view></div> <!-- 视图 -->
  16. </div>
  17. </body>
  18. <script type="text/javascript">
  19. //定义模板,并注入ui-router
  20. var app = angular.module('myApp', ['ui.router']);
  21. //对服务进行参数初始化,这里配stateProvider服务的视图控制
  22. app.config(["$stateProvider", function ($stateProvider) {
  23. $stateProvider
  24. .state("home", {
  25. url: '/',
  26. template:'<div>模板内容......</div>'
  27. })
  28. }]);
  29. </script>
  30. </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
  • 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. <script type="text/javascript">
  9. var app = angular.module('myApp', ['ui.router']);
  10. app.config(["$stateProvider", function ($stateProvider) {
  11. $stateProvider
  12. .state("parent", {//父路由
  13. url: '/parent',
  14. template:'<div>parent'
  15. +'<div ui-view><div>'// 子View
  16. +'</div>'
  17. })
  18. .state("parent.child", {//子路由
  19. url: '/child',
  20. template:'<div>child</div>'
  21. })
  22. }]);
  23. </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
  • 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
  • 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. <script type="text/javascript">
  10. var app = angular.module('myApp', ['ui.router']);
  11. app.config(["$stateProvider", function ($stateProvider) {
  12. $stateProvider
  13. .state("index", {
  14. url: '/index',
  15. views:{
  16. 'header':{template:"<div>头部内容</div>"},
  17. 'nav':{template:"<div>菜单内容</div>"},
  18. 'body':{template:"<div>展示内容</div>"}
  19. }
  20. })
  21. }]);
  22. </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
  • 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. <script type="text/javascript">
  10. var app = angular.module('myApp', ['ui.router']);
  11. app.config(["$stateProvider", function ($stateProvider) {
  12. $stateProvider
  13. .state("index", {
  14. url: '/index',
  15. views:{
  16. 'index':{template:"<div><div ui-view='header'></div> <div ui-view='nav'></div> <div ui-view='body'></div> </div>"},
  17. //这里必须要绝对定位
  18. 'header@index':{template:"<div>头部内容header</div>"},
  19. 'nav@index':{template:"<div>菜单内容nav</div>"},
  20. 'body@index':{template:"<div>展示内容contents</div>"}
  21. }
  22. })
  23. //绝对定位
  24. .state("index.content1", {
  25. url: '/content1',
  26. views:{
  27. 'body@index':{template:"<div>content11111111111111111</div>"}
  28. //'body@index'表时名为body的view使用index模板
  29. }
  30. })
  31. //相对定位:该状态的里的名为body的ui-view为相对路径下的(即没有说明具体是哪个模板下的)
  32. .state("index.content2", {
  33. url: '/content2',
  34. views:{
  35. 'body':{template:"<div>content2222222222222222222</div>"}//
  36. }
  37. })
  38. }]);
  39. </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
  • 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需要用@来绝对定位。

6.URL路由传参(通过$stateParams服务获取参数)

url: '/index/:id',url: '/index/{id}',两种形式传参

  1. <body >
  2. <div ng-app="myApp" >
  3. <a ui-sref="index({id:30})">show index</a>
  4. <a ui-sref="test({username:'peter'})">show test</a>
  5. <div ui-view></div>
  6. </div>
  7. </body>
  8. <script type="text/javascript">
  9. var app = angular.module('myApp', ['ui.router']);
  10. app.config(["$stateProvider", function ($stateProvider) {
  11. $stateProvider
  12. .state("home", {
  13. url: '/',
  14. template:"<div>homePage</div>"
  15. })
  16. .state("index", {
  17. url: '/index/:id',
  18. template:"<div>indexcontent</div>",
  19. controller:function($stateParams){
  20. alert($stateParams.id)
  21. }
  22. })
  23. .state("test", {
  24. url: '/test/:username',
  25. template:"<div>testContent</div>",
  26. controller:function($stateParams){
  27. alert($stateParams.username)
  28. }
  29. })
  30. }]);
  31. </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
  • 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

7.Resolve(预载入)

参考资料:

使用预载入功能,开发者可以预先载入一系列依赖或者数据,然后注入到控制器中。在ngRoute中resolve选项可以允许开发者在路由到达前载入数据保证(promises)。在使用这个选项时比使用angular-route有更大的自由度。

预载入选项需要一个对象,这个对象的key即要注入到控制器的依赖,这个对象的value为需要被载入的factory服务。

如果传入的时字符串,angular-route会试图匹配已经注册的服务。如果传入的是函数,该函数将会被注入,并且该函数返回的值便是控制器的依赖之一。如果该函数返回一个数据保证(promise),这个数据保证将在控制器被实例化前被预先载入并且数据会被注入到控制器中。

  1. <body >
  2. <div ng-app="myApp" >
  3. <a ui-sref="index">show index</a>
  4. <div ui-view></div>
  5. </div>
  6. </body>
  7. <script type="text/javascript">
  8. var app = angular.module('myApp', ['ui.router']);
  9. app.config(["$stateProvider", function ($stateProvider) {
  10. $stateProvider
  11. .state("home", {
  12. url: '/',
  13. template:"<div>homePage</div>"
  14. })
  15. .state("index", {
  16. url: '/index/{id}',
  17. template:"<div>indexcontent</div>",
  18. resolve: {
  19. //这个函数的值会被直接返回,因为它不是数据保证
  20. user: function() {
  21. return {
  22. name: "peter",
  23. email: "audiogroup@qq.com"
  24. }
  25. },
  26. //这个函数为数据保证, 因此它将在控制器被实例化之前载入。
  27. detail: function($http) {
  28. return $http({
  29. method: 'JSONP',
  30. url: '/current_details'
  31. });
  32. },
  33. //前一个数据保证也可作为依赖注入到其他数据保证中!(这个非常实用)
  34. myId: function($http, detail) {
  35. $http({
  36. method: 'GET',
  37. url: 'http://facebook.com/api/current_user',
  38. params: {
  39. email: currentDetails.data.emails[0]
  40. }
  41. })
  42. }
  43. },
  44. controller:function(user,detail,myId$scope){
  45. alert(user.name)
  46. alert(user.email)
  47. console.log(detail)
  48. }
  49. })
  50. }]);
  51. </script>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/1009293
推荐阅读
相关标签
  

闽ICP备14008679号