博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[AngularJS] ui-router: named views
阅读量:6830 次
发布时间:2019-06-26

本文共 3726 字,大约阅读时间需要 12 分钟。

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('/');    });

 

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: '
Sub One
' }, // find the 'alt-three' directory to replace the name view == "two" 'two@app.alt-three': { template: '
Sub Two
' } } } ) });

 

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

转载地址:http://upjkl.baihongyu.com/

你可能感兴趣的文章
项目中如果管理前端文件CSS和JS
查看>>
13 jsp include
查看>>
Nginx和PHP-FPM的启动、重启、停止脚本分享(转)
查看>>
如何拷贝CMD命令行文本到粘贴板
查看>>
Oracle数据库—— 存储过程与函数的创建
查看>>
兼容iOS 10 资料整理笔记
查看>>
逻辑回归原理小结
查看>>
php 7.0 安装以及老版本php删除
查看>>
【Machine Learning】决策树案例:基于python的商品购买能力预测系统
查看>>
servlet生成验证码
查看>>
oracle存储过程中使用execute immediate执行sql报ora-01031权限不足的问题
查看>>
Zing 5.0发布,包含原生支持Linux的无停顿垃圾回收器
查看>>
解决Fiddler不能监听Java HttpURLConnection请求的方法
查看>>
串的模式匹配算法---Horspool
查看>>
Python基础07 函数
查看>>
Android学习
查看>>
Windows Phone 7 EKB系列文章发布
查看>>
Jmeter(六)-关联设置
查看>>
Mvc 3 中的分部视图
查看>>
android学习笔记之ProgressDialog的使用
查看>>