angular UI-Router示例一

一、选择angular-ui-router的好处

  1. ui-router是一个社区库用来完善ng-route的诸多不足
  2. UI-Router路由器允许嵌套视图(nested views)和多个命名视图(multiple named views),我们可能有较多的页面需要继承其他部分,所以很有用。
  3. 通过构建ui-sref来实现不同的状态链接到不同的页面
  4. states允许你通过$statsParams来轻松的传递信息,允许不同的信息不同的states的map格式。
  5. 你的路由可以访问动态创建的链接

二、简单示例,步骤如下:

  1. 新建index.html

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <script src="js/angular.js"></script>
    <script src="js/angular-ui-router.js"></script>
    <script src="js/app.js"></script>
    
    </head>
    <body ng-app="myApp">
        <h1>AngularJS Home Page</h1>
        <div ui-view=""></div>
    </body>
    <html>
  2. 新建PageTab.html

    当我们想要在母版页中管理所有的页面时,我们就会想要一个叫做”ui-view“的占位标记, 因此我们现在把PageTab.html叫做一个母版页,因为它会把我们需要在PageTab.html中用”ui-view“ 声明好的其它页面都管理起来.

    <div>
        <div>
            <span style="width: 100px" ui-sref=".Page1"><a href="">Page-1</a></span>
            <span style="width: 100px" ui-sref=".Page2"><a href="">Page-2</a></span>
            <span style="width: 100px" ui-sref=".Page3"><a href="">Page-3</a></span>
        </div>
        <div>
            <div ui-view="" />
        </div>
    </div>

    使用ui-sref属性可以将App.js中定义的状态同tab中定义的对应文本进行了关联. 当我们使用点符号对它进行了声明,程序就会认为页面时ui-view中的子页面或者说嵌入页面,它们就是路由配置中需要被展示的页面.

    使用 . 号进行了分隔. 这里很关键,它会告诉路由引擎我们在这里定义的是子页面或者嵌入页面。

  3. 新建Page1.html Page2.html Page3.html
    <div >
         <div>
             <h1>Page 1 content goes here...</h1>
         </div>
    </div>

    其他2个同上所示。

  4. 在index.html中引入angular-ui-router.js 参考步骤1.
  5. 新建App.js

    谁来告诉程序应该显示哪个页面呢? 这就是我们要在路由引擎里面配置的东西

    var myApp = angular.module("myApp", [ 'ui.router' ]);
    
    myApp.config(function($stateProvider, $urlRouterProvider) {
    
        $urlRouterProvider.when("", "/PageTab");
    
        $stateProvider.state("PageTab", {
            url : "/PageTab",
            templateUrl : "PageTab.html"
        }).state("PageTab.Page1", {
            url : "/Page1",
            templateUrl : "Page1.html"
        }).state("PageTab.Page2", {
            url : "/Page2",
            templateUrl : "Page2.html"
        }).state("PageTab.Page3", {
            url : "/Page3",
            templateUrl : "Page3.html"
        });
    });
时间: 2024-08-02 06:42:34

angular UI-Router示例一的相关文章

angular+ui router+require

index.html !DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <link rel="stylesheet" href="../lib/bootstrap/css/bootstrap.min.css"></h

angular 的ui.router 定义不同的state 对应相同的url

Angular UI Router: Different states with same URL? The landing page of my app has two states: home-public, home-logged-in. Now I want to show both states on the same URL, but let the controller and template depend on the user session (is the user log

angular : $location &amp; $state(UI router)的关系

次序:angular 的 location会先跑 $rootScope.$on("$locationChangeStart", function (scope, newUrl, oldUrl) { log("$locationChangeStart from " + oldUrl) log("$locationChangeStart to " + newUrl) }); 接着是 ui router $state $rootScope.$on(&q

Angularjs中UI Router全攻略

摘自:Angularjs中UI Router全攻略 温馨提示:想要了解 angular-ui-router的同学,从上往下读一遍,能带随着coding那就更好了,保证你对angular-ui-router基本全部掌握. 如何引用依赖angular-ui-router angular.module('app',["ui.router"]) .config(function($stateProvider){ $stateProvider.state(stateName, stateCofi

ngRoute 和 ui.router 的使用方法和区别

在单页面应用中要把各个分散的视图给组织起来是通过路由机制来实现的.本文主要对 AngularJS 原生的 ngRoute 路由模块和第三方路由模块 ui.router 的用法进行简单介绍,并做一个对比. ngRoute 使用方法 1) 引入 angular-route lib 无论是 ngRoute 还是 ui.router ,作为框架额外的附加功能,都必须以 模块依赖 的形式被引入. 1 <script src="lib/angular-route.js"></sc

Angularjs中UI Router用法小记录

今天自己参考已有的项目代码学习了下UI Router的用法,写了个小demo,验证了下自己的想法,现把使用情况记录一下. 1.入口文件index.html,引入项目所需的js文件,标注ng-app,创建ui-view元素,为后面的嵌套做容器准备. <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <meta charset="UTF-8">

[转]AngularJS 使用 UI Router 实现表单向导

本文转自:http://www.oschina.net/translate/angularjs-multi-step-form-using-ui-router 今天我们将使用AngularJs和伟大的UI Router以及Angular ngAnimate module创建一个带动画的多步表单.这项技术可以用在你想要简化用户操作的大表单上. 我们看到这项技术已经应用在了许多的网页上.比如购物车,注册表单,入职流程以及许多多步表单,让用户更容易在线填写表单. 下面我们将构建它: 使用UI Rout

[Angular2 Router] Configure Auxiliary Routes in the Angular 2 Router - What is the Difference Towards a Primary Route?

In this tutorial we are going to learn how we can can configure redirects in the angular 2 router configuration. We are also going to see again another example on how the order of the configuration might trip us. We are going to see the different bet

[Angular 2 Router] Configure Your First Angular 2 Route

Using the Angular 2 router requires defining routes, passing them in to the RouterModule.forRoot and then importing the configured RouterModule into your main App Module. Use the Wiki Search as example project. Create a HomeComponent to contain every

Kendo UI常用示例汇总(六)

Kendo UI Professional 提供开源和商业两个版本.开源版 Kendo UI Core,有40+个框架和组件:商业版整合了之前的Kendo UI Web.Kendo UI Mobile 和 Kendo UI DataViz ,一共有70+个框架和组件.作为Kendo UI的升级版,Kendo UI Professional既可以开发网页版应用程序,也可以开发移动版应用程序,并且在性能上也有显著的优化和提升. Kendo UI Professional试用版下载猛戳>> Kend