[AngularJS] ui-router: named views
2014-12-17 17:03
344 查看
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'.
Result:
![](http://images.cnitblog.com/blog/364241/201412/171623202655077.jpg)
Now when we click 'One', 'Two' and 'Three', we also want to replace the content accordingly.
alt-one.js:
![](http://images.cnitblog.com/blog/364241/201412/171639312508485.jpg)
alt-two.js: we replace the content and header both at the same time.
![](http://images.cnitblog.com/blog/364241/201412/171645485155708.jpg)
alt-three.js:
![](http://images.cnitblog.com/blog/364241/201412/171702525786499.jpg)
Read More: https://egghead.io/lessons/angularjs-ui-router-named-views
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:
![](http://images.cnitblog.com/blog/364241/201412/171623202655077.jpg)
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' } } }) })
![](http://images.cnitblog.com/blog/364241/201412/171639312508485.jpg)
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' } } }) })
![](http://images.cnitblog.com/blog/364241/201412/171645485155708.jpg)
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>' } } } ) }) ;
![](http://images.cnitblog.com/blog/364241/201412/171702525786499.jpg)
Read More: https://egghead.io/lessons/angularjs-ui-router-named-views
相关文章推荐
- AngularJS—学习 ui-router - 路由控制
- AngularJS进阶(一)深入理解ANGULARUI路由_UI-ROUTER
- --@angularJS--路由插件UI-Router
- Angularjs1.0x中使用ui-router路由带参跳转的比较好的实现
- AngularJS—学习 ui-router - 组件
- angularjs ie8 router-ui 不支持属性和方法
- AngularJS进阶(一)深入理解ANGULARUI路由_UI-ROUTER
- [转]AngularJS 使用 UI Router 实现表单向导
- angularJS中的ui-router和ng-grid模块
- AngularJs之ui.router路由基本配置及传参方法
- angularJS之ui-router
- AngularJS1.6版本中ui-router路由中/#!/的解决方法
- AngularJS路由之ui-router(三)大小写处理
- 深入理解AngularJs-Ui-router
- AngularJS 路由:ng-route 与 ui-router
- AngularJS中ui-router全攻略
- AngularJS中ui-router全攻略
- AngularJS 使用 UI Router 实现表单向导
- AngularJs学习之ui-router深入 组件
- 关于AngularJS的ui-router