ui-router的工作原理非常类似于Angular的路由控制,他只关注状态。

Angular模板

最简单的模板,例如main.html:

<body data-ng-app="myApp">
<h1>AngularJS Home Page</h1>
<div ui-view></div>
</body>

ui-view代表的是一个模板的占位符,例如要在ui-view下插入内容,可以通过如下代码:

var myapp=angular.module("myApp",["ui.router"]);
myapp.config(function($stateProvider,$urlRouterProvider){ $urlRouterProvider.when("","/pagetab");
$stateProvider.state("pagetab",{
url:"/pagetab",
template:"<h1>hello,AngularJs</h>"
})
});

当程序运行时,在模板中插入一段代码:效果如下:

模板将被插入哪里?

状态被激活时,它的模板会自动插入到父状态对应模板中包含 ui-view属性的 元素内部 ,如果是顶层状态,那么它的父模板就是index.html

激活状态

1)调用$state.go()方法

2)点击包含ui-self指令的链接;

3)导航到状态相关联的url

Templates模板

方法一:配置template属性,指定一段html字符串,例如:

$stateProvider.state("pagetab",{
url:"/pagetab",
template:"<h1>hello,AngularJs</h>"
})

方法二:通过配置templateUrl属性,来加载对应的模板,例如:

$stateProvider.state("pagetab",{
url:"/pagetab",
templateUrl:"pagetab.html"
})

更多模板配置方法请参考:http://bubkoo.com/2014/01/01/angular/ui-router/guide/state-manager/

综合模板与路由的知识,做一个简单的路由demo,包含的页面有main.html,pagetab.html,page1.html,page2.html,page3.html

main.html的代码如下:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Book Store</title>
<script src="Scripts/angular.js"></script>
<script src="Scripts/angular-ui-router.js"></script>
<script src="Scripts/app.js"></script>
</head> <body data-ng-app="myApp">
<h1>AngularJS Home Page</h1>
<div ui-view></div>
</body>
</html>

pagetab.html代码如下,通过设置ui-sref导航到对应的page1等:

<div>
<div>
<span style="width:100px" ui-sref=".page1"><a href="">Page1</a></span>
<span style="width:100px" ui-sref=".page2"><a href="">Page2</a></span>
<span style="width:100px" ui-sref=".page3"><a href="">Page3</a></span>
</div>
<div>
<div ui-view=""></div>
</div>
</div>

page1.html:

<div>
<div>
<h1>Page 1 content goes here........</h1>
</div>
</div>

page2.html:

<div>
<div>
<h1>Page 2 content goes here........</h1>
</div>
</div>

page3.html:

<div>
<div>
<h1>page 3 content goes here......</h1>
</div>
</div>

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"
})
});

对应的效果为:

点击page1,会把page1对应的html包含在pagetab.html对应的ui-view中。

Angular-ui-router路由,View管理的更多相关文章

  1. angular ui.router 路由传参数

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

  2. Angularjs ui router,路由嵌套 父controller执行问题

    解决方式来源:https://stackoverflow.com/questions/25316591/angularjs-ui-router-state-reload-child-state-onl ...

  3. 规范 : angular ui router path & params

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

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

    Angular UI Router: Different states with same URL? The landing page of my app has two states: home-p ...

  5. 前端MVC Vue2学习总结(八)——Vue Router路由、Vuex状态管理、Element-UI

    一.Vue Router路由 二.Vuex状态管理 三.Element-UI Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌面端UI框架,手机端有对应框架是 Mint U ...

  6. angular : $location & $state(UI router)的关系

    次序:angular 的 location会先跑 $rootScope.$on("$locationChangeStart", function (scope, newUrl, o ...

  7. angular当router使用userhash:false时路由404问题

    angular当router使用userhash:false时路由404问题 安装iis urlrewrite2.0组件 在根目录下创建 Web.config <configuration> ...

  8. Angular 从入坑到挖坑 - Router 路由使用入门指北

    一.Overview Angular 入坑记录的笔记第五篇,因为一直在加班的缘故拖了有一个多月,主要是介绍在 Angular 中如何配置路由,完成重定向以及参数传递.至于路由守卫.路由懒加载等&quo ...

  9. 【原创】ui.router源码解析

    Angular系列文章之angular路由 路由(route),几乎所有的MVC(VM)框架都应该具有的特性,因为它是前端构建单页面应用(SPA)必不可少的组成部分. 那么,对于angular而言,它 ...

随机推荐

  1. CSS div固定顶端

    position: fixed;原来只需要这么一个设置就可以!

  2. Python调用API接口的几种方式 数据库 脚本

    Python调用API接口的几种方式 2018-01-08 gaoeb97nd... 转自 one_day_day... 修改 微信分享: 相信做过自动化运维的同学都用过API接口来完成某些动作.AP ...

  3. git add . git add -u git add -A命令区别图解

    git版本不同会有所区别: Git Version 1.x:  Git Version 2.x:  git add .  修改(modified)以及新文件(new),但不包括被删除的文件. git ...

  4. Java 异常介绍

    Java标准库内建了一些通用的异常,这些类以 Throwable 为顶层父类.Throwable又派生出 Error 类和 Exception 类. 错误:Error类以及他的子类的实例,代表了JVM ...

  5. Disruptor 创建过程

    1 Disruptor disruptor = new Disruptor<ValueEvent>(ValueEvent.EVENT_FACTORY, ringBufferSize, ex ...

  6. Linux进程间通信(一) - 管道

    管道(pipe) 普通的Linux shell都允许重定向,而重定向使用的就是管道. 例如:ps | grep vsftpd .管道是单向的.先进先出的.无结构的.固定大小的字节流,它把一个进程的标准 ...

  7. spring BeanFactory加载xml配置文件示例

    项目目录结构如下: HelloWorld.java package com.thief.demo; public class HelloWorld { public void sayHello() { ...

  8. HTML 与 SGML关系

    HTML :超文本标记语言,“超文本”就是指页面内可以包含图片.链接,甚至音乐.程序等非文字元素 SGML:标准通用标记语言 HTML 是SGML下的一个应用

  9. 虚拟机 minimal 安装增强包

    在虚拟机下安装了一个centos的minimal镜像,发现增强包不能安装,鼠标不能在虚拟机和物理机间自由切换.不能共享粘贴板,非常是不爽,这里摸索出在centos  minimal OS下安装增强包的 ...

  10. openstack 官方镜像qcow2 下载和修改密码

    下载地址: CentOS6:http://cloud.centos.org/centos/6/images/ CentOS7:http://cloud.centos.org/centos/7/imag ...