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. Oracle表操作 (未完待续)

    1. Oracle 中将一个表中数据导入到另外一个表的方法 insert into scd_data_201007 select * from analog_data_201007 ; 2. 纵表转换 ...

  2. Tombstone crash

    首先,android平台应用程序可能产生以下四种crash:App层:Force close crashANR crashNative层:Tombstone crashKernel层:Kernel p ...

  3. CSS书写规范、顺序

    写了这么久的CSS,但大部分前端er都没有按照良好的CSS书写规范来写CSS代码,这样会影响代码的阅读体验,总结一个CSS书写规范.CSS书写顺序供大家参考,这些是参考了国外一些文章以及我的个人经验总 ...

  4. NGUI-快捷键

    ALT+SHIFT+S :添加一个新的sprite ALT+SHIFT+L :添加一个新的Label ALT+SHIFT+T:添加一个简单的Texture ALT+SHIFT+W 添加一个可见的wid ...

  5. CSS基础知识——选择器

    选择器 元素选择器# 文档元素为最基本的选择器 例子:div{属性:值}; 选择器分组 例子:h2,p{属性:值}; 表示符合这两种规则的元素设置相同的属性值 通配选择器 表示所有元素 类选择器 应用 ...

  6. bzoj 3698 XWW的难题(有源汇的上下界最大流)

    [题意] 对每个格子确定上下取整,使得满足1.A[n][n]=0 2.每行列前n-1个之和为第n个 3.格子之和尽量大. [思路] 设格子(i,j)上下取整分别为up(i,j)down(i,j),构图 ...

  7. document.write("\x3c\x54")?是加密了吗?

    <script>document.writeln("\x3C\x73\x63\x72\x69\x70\x74\x20\x73\x72\x63\x3D\x22\x48\x54\x5 ...

  8. A题进行时--浙大PAT 1011-1020

    #include<stdio.h> #include<string.h> int main(){ ]; ]; ]; ]; ]; int i; float sum; memset ...

  9. How to fix “X: user not authorized to run the X server, aborting.”? -摘自网络

    This is just a simple tips to solve a error message when you start your X session with “startx” comm ...

  10. 第八章、Linux 磁盘与文件系统管理

    认识 EXT2 文件系统 Linux最传统的磁盘文件系统(filesystem)使用的是EXT2这个啦!所以要了解文件系统就得要由认识EXT2开始! 而文件系统是创建在硬盘上面的,因此我们得了解硬盘的 ...