[AngularJS] Introduction to ui-router
Introduce to basic $stateProvider.state() with $stateParams services. Understand how nested router works.
Key Value:
ng-href="#/list/{{item.name}}"
.state('list.item', {
url: '/:item',
templateUrl: 'templates/list.item.tmpl.html',
controller: 'ItemCtrl',
controllerAs: 'item'
})
ui-sref="list.item({item: item.name})"
the same as
ui-sref=".item({item: item.name})" <!-- ui.route understand to find the parent router-->
Note: we can put template into a spreated html, here we just put inside index.html and use type to define it.
script type="text/ng-template"
<!DOCTYPE html>
<html ng-app="app">
<head> <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css"/> <script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.11/angular-ui-router.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div class="container">
<h4>
A brief introduction to <strong class="text-danger">ui-router</strong>
<span class="text-muted">(v0.2.0)</span>
</h4> <div>
<ul class="nav nav-pills">
<li><a ui-sref="home">Home</a></li>
<li><a ui-sref="list">Shopping List</a></li>
</ul>
</div>
<div ui-view></div>
</div> <script type="text/ng-template" id="templates/home.tmpl.html">
<div class="row">
<h3>What is ui-router?</h3> <p>URL routing is a popular approach to matching the contents of a URL to specific functionality within a web
application. URL routes programmatically present specific content to users based on the URL that they are
visiting. It is a popular approach that has proven to be very effective.</p> <P>Something that might not be obvious is that URL routing is also a finite state machine. When you configure
the routing for an app, you are laying out the various states the application can be in, and informing the
application what to display and do when a specific route is encountered.</P> <p>AngularJS supplies URL routing by default. It is adequate, but also has some limitations.</p>
</div>
</script> <script type="text/ng-template" id="templates/list.tmpl.html">
<div class="row padded">
<div class="list-group col-xs-3">
<a class="list-group-item"
ng-repeat="item in list.shoppingList"
ng-class="{active: item.selected}"
ng-href="#/list/{{item.name}}"
ng-click="list.selectItem(item)">{{item.name}}</a>
</div>
<div ui-view class="col-xs-9"></div>
</div>
</script> <script type="text/ng-template" id="templates/list.item.tmpl.html">
<h3>{{item.item}}</h3>
<img ng-src="//robohash.org/{{item.item}}.png"/>
</script> <script src="app.js"></script>
</body>
</html>
"ui-view" is important to tell where ui-router should show the view.
/**
* Created by Answer1215 on 12/16/2014.
*/
angular.module('app', ['ui.router'])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'templates/home.tmpl.html'
})
.state('list', {
url: '/list',
templateUrl: 'templates/list.tmpl.html',
controller: 'ListCtrl',
controllerAs: 'list'
})
//nested router: "list.item",
// ui-router understands that item is under list parent.
.state('list.item', {
url: '/:item',
templateUrl: 'templates/list.item.tmpl.html',
controller: 'ItemCtrl',
controllerAs: 'item'
})
}) .controller('ListCtrl', ListCtrl) .controller('ItemCtrl', ItemCtrl) function ItemCtrl($stateParams) { var ItemCtrl = this;
ItemCtrl.item = $stateParams.item;
} function ListCtrl() { var ListCtrl = this;
ListCtrl.shoppingList = [
{name: 'Milk'},
{name: 'Eggs'},
{name: 'Bread'},
{name: 'Cheese'},
{name: 'Ham'}
]; ListCtrl.selectItem = function(selectedItem) {
_(ListCtrl.shoppingList).each(function(item) {
item.selected = false;
if(selectedItem === item) {
selectedItem.selected = true;
}
});
};
}
Read More: https://egghead.io/lessons/angularjs-introduction-ui-router
[AngularJS] Introduction to ui-router的更多相关文章
- angularjs ngRoute和ui.router对比
ngRoute模块是angularjs自带的路由模块,ui.router是一个第三方路由模块,接下来将对两者进行一个对比: ng-router(angular-router.js) ng-view n ...
- angularjs的路由ui.router
<!-- 引入路由插件 --> <script src="vendor/angular-ui-router/release/angular-ui-router.min. ...
- AngularJS学习之 ui router
1.安装 bower install --save angular_ui-router 2.在项目主页面 index.html中添加 <div ui-view="">& ...
- AngularJS 使用 UI Router 实现表单向导
Today we will be using AngularJS and the great UI Router and the Angular ngAnimate module to create ...
- [转]AngularJS 使用 UI Router 实现表单向导
本文转自:http://www.oschina.net/translate/angularjs-multi-step-form-using-ui-router 今天我们将使用AngularJs和伟大的 ...
- [转]AngularJS+UI Router(1) 多步表单
本文转自:https://www.zybuluo.com/dreamapplehappy/note/54448 多步表单的实现 在线demo演示地址https://rawgit.com/dream ...
- angularJS ui router 多视图单独刷新问题
场景:视图层级如下 view1 --view11 --view111 需求:view11的一个动作过后,单独刷新view12 解决方式:修改层级设计 view1 --view11 --view111 ...
- ngRoute 和 ui.router 的使用方法和区别
在单页面应用中要把各个分散的视图给组织起来是通过路由机制来实现的.本文主要对 AngularJS 原生的 ngRoute 路由模块和第三方路由模块 ui.router 的用法进行简单介绍,并做一个对比 ...
- 【原创】ui.router源码解析
Angular系列文章之angular路由 路由(route),几乎所有的MVC(VM)框架都应该具有的特性,因为它是前端构建单页面应用(SPA)必不可少的组成部分. 那么,对于angular而言,它 ...
随机推荐
- Android:实现一种浮动选择菜单的效果
总结如何实现Android浮动层,主要是dialog的使用. 自定义一个类继承自Dialog类,然后在构造方法中,定义这个dialog的布局和一些初始化信息. 案例1: public class Me ...
- Android Dialog用法
摘要: 创建对话框 一个对话框一般是一个出现在当前Activity之上的一个小窗口. 处于下面的Activity失去焦点, 对话框接受所有的用户交互. 对话框一般用于提示信息和与当前应用程序直接相关的 ...
- 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(3)
运行ElasticSearch(Running ElasticSearch) 让我们运行我们的第一个实例.转到bin目录并从命令行运行以下命令: ./elasticsearch –f (Linux o ...
- SQL经典笔试题之一
本题用到下面三个关系表: CARD 借书卡. CNO 卡号,NAME 姓名,CLASS 班级 BOOKS 图书. BNO 书号,BNAME 书名,AUTHOR 作者,PRIC ...
- jquery的each()函数用法
each()方法能使DOM循环结构简洁,不容易出错.each()函数封装了十分强大的遍历功能,使用也很方便,它可以遍历一维数组.多维数组.DOM, JSON 等等 在javaScript开发过程中使用 ...
- mybatis系列-04-mybatis开发dao的方法
4.1 SqlSession使用范围 4.1.1 SqlSessionFactoryBuilder 通过SqlSessionFactoryBuilder创建会话工厂SqlSession ...
- Unbutu网卡驱动安装(Intel内置网卡8086:15b8)
工作中需要在新的实验平台上安装unbuntu14.04操作系统,系统安装好之后发现无法连接网络,分析后是由于缺少网卡驱动的原因. 下面把分析问题的过程及安装网卡驱动步骤介绍如下: 查看PCI信息 su ...
- java动态代理与老式AOP实现
JAVA的动态代理 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托类之间通常会 ...
- CodeForces 567A Gerald is into Art
http://codeforces.com/problemset/problem/567/A A. Lineland Mail time limit per test 3 seconds memory ...
- Codeforces Round #245 (Div. 1) B. Working out (简单DP)
题目链接:http://codeforces.com/problemset/problem/429/B 给你一个矩阵,一个人从(1, 1) ->(n, m),只能向下或者向右: 一个人从(n, ...