The ui-router library for AngularJS provides the ability to name views within your application. This is useful for dividing up your application into sections, and changing the content of a section based on the current state.

We use named view to build a simple webpage with 'header','sidebar','content' and 'footer'.

/**
* Created by Answer1215 on 12/17/2014.
*/
angular.module('app', ['ui.router'])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: '/',
views: {
'header': {
templateUrl: 'app/common/header.tpl.html'
},
'sidebar': {
templateUrl: 'app/common/sidebar.tpl.html'
},
'content': {
templateUrl: 'app/common/content.tpl.html'
},
'footer': {
templateUrl: 'app/common/footer.tpl.html'
}
}
});
$urlRouterProvider.otherwise('/');
});
<div class="container">

    <!-- Header -->
<div ui-view="header" class="row"></div> <div class="row">
<!-- Sidebar/Nav -->
<div ui-view="sidebar" class="col-xs-3"></div>
<!-- Content -->
<div ui-view="content" class="col-xs-9"></div>
</div> <!-- Footer -->
<div ui-view="footer" class="row"></div>
</div>

Result:

Now when we click 'One', 'Two' and 'Three', we also want to replace the content accordingly.

alt-one.js:

/**
* Created by Answer1215 on 12/17/2014.
*/
angular.module('app.alt-one', ['ui.router'])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app.alt-one', {
url: 'alt-one',
views: {
// '@': replace the content
// if there is just @ without other stuff, it will looking for the parent 'app' root
'content@': {
templateUrl: 'app/alt-one/alt-one.content.tpl.html'
}
}
})
})

alt-two.js: we replace the content and header both at the same time.

/**
* Created by Answer1215 on 12/17/2014.
*/
angular.module('app.alt-two', ['ui.router'])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app.alt-two', {
url: 'alt-two',
views: {
'content@': {
templateUrl: 'app/alt-two/alt-two.content.tpl.html'
},
'header@': {
templateUrl: 'app/alt-two/alt-two.header.tpl.html'
}
}
})
})

alt-three.js:

/**
* Created by Answer1215 on 12/17/2014.
*/
angular.module('app.alt-three', [
'ui.router'
])
.config(function($stateProvider) {
$stateProvider
.state('app.alt-three', {
url: 'alt-three',
views: {
'content@': {
templateUrl: 'app/alt-three/alt-three.content.tpl.html'
},
'header@': {
templateUrl: 'app/alt-three/alt-three.header.tpl.html'
},
// find the 'alt-three' directory to replace the name view == "one"
'one@app.alt-three': {
template: '<div class="alert-info">Sub One</div>'
},
// find the 'alt-three' directory to replace the name view == "two"
'two@app.alt-three': {
template: '<div class="alert-success">Sub Two</div>'
}
}
}
)
})
;

Read More: https://egghead.io/lessons/angularjs-ui-router-named-views

[AngularJS] ui-router: named views的更多相关文章

  1. angularJS ui router 多视图单独刷新问题

    场景:视图层级如下 view1 --view11 --view111 需求:view11的一个动作过后,单独刷新view12 解决方式:修改层级设计 view1 --view11 --view111 ...

  2. [转]AngularJS+UI Router(1) 多步表单

    本文转自:https://www.zybuluo.com/dreamapplehappy/note/54448 多步表单的实现   在线demo演示地址https://rawgit.com/dream ...

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

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

  4. AngularJS 使用 UI Router 实现表单向导

    Today we will be using AngularJS and the great UI Router and the Angular ngAnimate module to create ...

  5. [Angular-Scaled web] 4. Using ui-router's named views

    In the previous post, we use $stateProvider deriect to root url and using MainCtrl, categories.tmpl. ...

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

    本文转自:http://www.oschina.net/translate/angularjs-multi-step-form-using-ui-router 今天我们将使用AngularJs和伟大的 ...

  7. angularjs ngRoute和ui.router对比

    ngRoute模块是angularjs自带的路由模块,ui.router是一个第三方路由模块,接下来将对两者进行一个对比: ng-router(angular-router.js) ng-view n ...

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

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

  9. ngRoute 与ui.router区别

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

随机推荐

  1. 总结c++ primer中的notes

    转载:http://blog.csdn.net/ace_fei/article/details/7386517 说明: C++ Primer, Fourth Edition (中英文)下载地址:htt ...

  2. 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(5)

    数据操作与REST API(Data manipulation with REST API) ElasticSearch REST API可用于各种任务.多亏了它,我们可以管理索引,更改实例参数,检查 ...

  3. Ubuntu下安装JDK图文详解

    很详细的在Ubuntu中安装JDK图文详解教程,我们选择的是jdk1.6.0_30版本.安装文件名为jdk-6u30-linux-i586.bin. 1.复制jdk到安装目录 (1)假设jdk安装文件 ...

  4. C语言反转字符串

    也是面腾讯的一道编程题=,= 这题比较简单 代码如下: #include <stdio.h> #include <string.h> // 非递归实现字符串反转 char *r ...

  5. QT-【转】2D编程

    Qt中提供了强大的2D绘图系统,可以使用相同的API在屏幕上和绘图·设备上进行绘制,主要基于QPainter.QPainterDevice和QPainterEngine这3个类. 1.QPainter ...

  6. 分布式文件系统-HDFS

    HDFS Hadoop的核心就是HDFS与MapReduce.那么HDFS又是基于GFS的设计理念搞出来的. HDFS全称是Hadoop Distributed System.HDFS是为以流的方式存 ...

  7. algorithm@ Shortest Path in Directed Acyclic Graph (O(|V|+|E|) time)

    Given a Weighted Directed Acyclic Graph and a source vertex in the graph, find the shortest paths fr ...

  8. Sql建表语句

    create table dbo.[Finance_CityInfo] ([CityId] int identity(1,1) not null , [City] nvarchar(20) not n ...

  9. URAL 2040 Palindromes and Super Abilities 2 (回文自动机)

    Palindromes and Super Abilities 2 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/E Descr ...

  10. [iOS微博项目 - 1.6] - 自定义TabBar

    A.自定义TabBar 1.需求 控制TabBar内的item的文本颜色(普通状态.被选中状态要和图标一致).背景(普通状态.被选中状态均为透明) 重新设置TabBar内的item位置,为下一步在Ta ...