Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了UI-Router详解大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我们了解 angular.js 是一种富客户端单页面应用,所以要在一个页面呈现不同的视图,路由起到了至关重要的作用.

angular.js :为我们封装好了一个路由工具 ngRoute,它是一种靠url改变去驱动视图.

angularUI :也为我们封装了一个独立的路由模块 ui-router,它是一种靠状态 state 来驱动视图.

后者有什么优势:一个页面可以嵌套多个视图,多个视图去控制某一个视图等.

咱们今天主要讲解ui-router的基本模块,先上代码

<!DOCTYPE html>
<html lang="en" ng-app="sunday">
<head>
  <Meta charset="UTF-8">
  <title>Document</title>
</head>
<body>

  <a ui-sref="Hello" ui-sref-active="active">Hello</a>
  <br>
  <a ui-sref="world" ui-sref-active="active">world</a>

  <ui-view></ui-view>


  <script src="js/angular.js"></script>
  <script src="js/angular-ui-router.js"></script>
  <script src="js/index.js"></script>

</body>
</html>

基本的index.html,ui-sref表示指令链接一个特定的状态,一般情况下为替换视图,替换视图的部分为使用<ui-view></ui-view>标记的区域。可以简单的理解为<ui-view></ui-view> 起到的其实是一个占位符的作用。

接下来咱们再看js代码

(function(angular){ angular.module('sunday',['ui.router']) .config(function($stateProvider){ $stateProvider.state({ name:'Hello',url:'/Hello',template:'<h3>Hello world!</h3>' }).state({ name:'world',url:'/world',template:'<h3>Hello ui-router!</h3>' }) }); })(angular);

在构造angular对象的时候,我们引入了 'ui.router' 模块,在angular对象的配制方法中,我们通过 ui-router提供的$stateProvider对象的 state方法去配置路由对象,name对应ui-sref中配置的值,使用template去替换<ui-view></ui-view>

那么ui-router中的嵌套路由是如何使用的呢?来看我们修改之后的代码

.state('world',{
        url:'/world',templateUrl:'world.html'
      })

我们对index.js进行了修改,使点击world的之后指向了一个 world.html 的模板。

<!DOCTYPE html>
<html lang="en">
<head>
  <Meta charset="UTF-8">
  <title>This is a world</title>
</head>
<body>

    <div>
      <a ui-sref=".world1" ui-sref-active="active">world-1</a>
      <br>
      <a ui-sref="world2" ui-sref-active="active">world-2</a>
    </div>

    <div ui-view style="margin-left: 50px"></div>

</body>
</html>

这是world.html中的代码我们可以看到在world.html中我们创建了两个 ui-sref 分别为:.world1和world2 ,相信看到这里大家也都能知道了,ui-router其实就是通过 .语法 去进行的路由层级的配置。
来看一下新的 index.js的代码

(function(angular){ angular.module('sunday',['ui.router']) .config(function($stateProvider){ $stateProvider.state('Hello-world',{ url:'/Hello',template:'<h3>Hello world!</h3>' }).state('world',{ url:'/world',templateUrl:'world.html' }).state('world.world1',{ url:'/world/world-1',template:'<h3>This is a World 1</h3>' }).state('world2',{ url:'/world/world-2',template:'<h3>world2并不依赖于world,在我们点击它的时候,他会替换掉index.html中的<div ui-view></div></h3>' }) }); })(angular);

index.html的代码

<!DOCTYPE html>
<html lang="en" ng-app="sunday">
<head>
  <Meta charset="UTF-8">
  <title>Document</title>

  <style> </style>
</head>
<body>

  <a ui-sref="Hello-world" ui-sref-active="active">Hello</a>
  <br>
  <a ui-sref="world" ui-sref-active="active">world</a>

  <div ui-view style="margin-left: 50px"></div>


  <script src="js/angular.js"></script>
  <script src="js/angular-ui-router.js"></script>
  <script src="js/index.js"></script>

</body>
</html>

大佬总结

以上是大佬教程为你收集整理的UI-Router详解全部内容,希望文章能够帮你解决UI-Router详解所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。