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'.

  1. /**
  2. * Created by Answer1215 on 12/17/2014.
  3. */
  4. angular.module('app', ['ui.router'])
  5. .config(function($stateProvider, $urlRouterProvider) {
  6. $stateProvider
  7. .state('app', {
  8. url: '/',
  9. views: {
  10. 'header': {
  11. templateUrl: 'app/common/header.tpl.html'
  12. },
  13. 'sidebar': {
  14. templateUrl: 'app/common/sidebar.tpl.html'
  15. },
  16. 'content': {
  17. templateUrl: 'app/common/content.tpl.html'
  18. },
  19. 'footer': {
  20. templateUrl: 'app/common/footer.tpl.html'
  21. }
  22. }
  23. });
  24. $urlRouterProvider.otherwise('/');
  25. });
  1. <div class="container">
  2.  
  3. <!-- Header -->
  4. <div ui-view="header" class="row"></div>
  5.  
  6. <div class="row">
  7. <!-- Sidebar/Nav -->
  8. <div ui-view="sidebar" class="col-xs-3"></div>
  9. <!-- Content -->
  10. <div ui-view="content" class="col-xs-9"></div>
  11. </div>
  12.  
  13. <!-- Footer -->
  14. <div ui-view="footer" class="row"></div>
  15. </div>

Result:

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

alt-one.js:

  1. /**
  2. * Created by Answer1215 on 12/17/2014.
  3. */
  4. angular.module('app.alt-one', ['ui.router'])
  5. .config(function($stateProvider, $urlRouterProvider) {
  6. $stateProvider
  7. .state('app.alt-one', {
  8. url: 'alt-one',
  9. views: {
  10. // '@': replace the content
  11. // if there is just @ without other stuff, it will looking for the parent 'app' root
  12. 'content@': {
  13. templateUrl: 'app/alt-one/alt-one.content.tpl.html'
  14. }
  15. }
  16. })
  17. })

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

  1. /**
  2. * Created by Answer1215 on 12/17/2014.
  3. */
  4. angular.module('app.alt-two', ['ui.router'])
  5. .config(function($stateProvider, $urlRouterProvider) {
  6. $stateProvider
  7. .state('app.alt-two', {
  8. url: 'alt-two',
  9. views: {
  10. 'content@': {
  11. templateUrl: 'app/alt-two/alt-two.content.tpl.html'
  12. },
  13. 'header@': {
  14. templateUrl: 'app/alt-two/alt-two.header.tpl.html'
  15. }
  16. }
  17. })
  18. })

alt-three.js:

  1. /**
  2. * Created by Answer1215 on 12/17/2014.
  3. */
  4. angular.module('app.alt-three', [
  5. 'ui.router'
  6. ])
  7. .config(function($stateProvider) {
  8. $stateProvider
  9. .state('app.alt-three', {
  10. url: 'alt-three',
  11. views: {
  12. 'content@': {
  13. templateUrl: 'app/alt-three/alt-three.content.tpl.html'
  14. },
  15. 'header@': {
  16. templateUrl: 'app/alt-three/alt-three.header.tpl.html'
  17. },
  18. // find the 'alt-three' directory to replace the name view == "one"
  19. 'one@app.alt-three': {
  20. template: '<div class="alert-info">Sub One</div>'
  21. },
  22. // find the 'alt-three' directory to replace the name view == "two"
  23. 'two@app.alt-three': {
  24. template: '<div class="alert-success">Sub Two</div>'
  25. }
  26. }
  27. }
  28. )
  29. })
  30. ;

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. POJ 1080 Human Gene Functions

    题意:给两个DNA序列,在这两个DNA序列中插入若干个'-',使两段序列长度相等,对应位置的两个符号的得分规则给出,求最高得分. 解法:dp.dp[i][j]表示第一个字符串s1的前i个字符和第二个字 ...

  2. HDU 5119 Happy Matt Friends

    Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others ...

  3. java web 学习十五(jsp基础语法)

    任何语言都有自己的语法,JAVA中有,JSP虽然是在JAVA上的一种应用,但是依然有其自己扩充的语法,而且在JSP中,所有的JAVA语句都可以使用. 一.JSP模版元素 JSP页面中的HTML内容称之 ...

  4. 编写一个循环将list容器的元素逆序输出

    <c++ primer>P270,习题9.9 实现代码如下: #include<iostream> #include<list> using namespace s ...

  5. Linux下用Intel编译器编译安装NetCDF-Fortan库(4.2以后版本)

    本来这个问题真的没必要写的,可是真的困扰我太久%>_<%,决定还是记录一下. 首先,最权威清晰的安装文档还是官方的: Building the NetCDF-4.2 and later F ...

  6. CSS基础(背景、文本、列表、表格、轮廓)

    CSS 背景属性 属性 描述 background 简写属性,作用是将背景属性设置在一个声明中. background-attachment 背景图像是否固定或者随着页面的其余部分滚动. backgr ...

  7. YARN应用场景、原理与资源调度

    1.Hadoop YARN产生背景 源于MapReduce1.0 运维成本 如果采用“一个框架一个集群”的模式,则可能需要多个管理员管理这些集群,进而增加运维成本,而共享模式通常需要少数管理员即可完成 ...

  8. poj1001 Exponentiation

    Description Problems involving the computation of exact values of very large magnitude and precision ...

  9. 执行原始的 SQL 查询

    The Entity Framework Code First API includes methods that enable you to pass SQL commands directly t ...

  10. 5个Xcode开发调试技巧

    转自Joywii的博客,原文:Four Tips for Debugging in XCode Like a Bro    1.Enable NSZombie Objects(开启僵尸对象) Enab ...