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的更多相关文章

  1. angularjs ngRoute和ui.router对比

    ngRoute模块是angularjs自带的路由模块,ui.router是一个第三方路由模块,接下来将对两者进行一个对比: ng-router(angular-router.js) ng-view n ...

  2. angularjs的路由ui.router

      <!-- 引入路由插件 --> <script src="vendor/angular-ui-router/release/angular-ui-router.min. ...

  3. AngularJS学习之 ui router

    1.安装 bower install --save angular_ui-router 2.在项目主页面 index.html中添加 <div ui-view="">& ...

  4. AngularJS 使用 UI Router 实现表单向导

    Today we will be using AngularJS and the great UI Router and the Angular ngAnimate module to create ...

  5. [转]AngularJS 使用 UI Router 实现表单向导

    本文转自:http://www.oschina.net/translate/angularjs-multi-step-form-using-ui-router 今天我们将使用AngularJs和伟大的 ...

  6. [转]AngularJS+UI Router(1) 多步表单

    本文转自:https://www.zybuluo.com/dreamapplehappy/note/54448 多步表单的实现   在线demo演示地址https://rawgit.com/dream ...

  7. angularJS ui router 多视图单独刷新问题

    场景:视图层级如下 view1 --view11 --view111 需求:view11的一个动作过后,单独刷新view12 解决方式:修改层级设计 view1 --view11 --view111 ...

  8. ngRoute 和 ui.router 的使用方法和区别

    在单页面应用中要把各个分散的视图给组织起来是通过路由机制来实现的.本文主要对 AngularJS 原生的 ngRoute 路由模块和第三方路由模块 ui.router 的用法进行简单介绍,并做一个对比 ...

  9. 【原创】ui.router源码解析

    Angular系列文章之angular路由 路由(route),几乎所有的MVC(VM)框架都应该具有的特性,因为它是前端构建单页面应用(SPA)必不可少的组成部分. 那么,对于angular而言,它 ...

随机推荐

  1. java文件过滤器

    java中有一个FilenameFilter的接口,能够过滤得到指定类型的文件或者目录,其中必须重写accept(File file,String path)方法 public class DirFi ...

  2. 【原】Storm学习资料推荐

    4.Storm学习资料推荐 书籍: 英文: Learning Storm: Ankit Jain, Anand Nalya: 9781783981328: Amazon.com: Books Gett ...

  3. python中yield用法

    在介绍yield前有必要先说明下Python中的迭代器(iterator)和生成器(constructor). 一.迭代器(iterator) 在Python中,for循环可以用于Python中的任何 ...

  4. Google Chart API 参考 中文版

    Google Chart API 参考 中文版 文档信息 翻译: Cloudream ,最后修改:02/22/2008 06:11:08 英文版版权归 Google , 转载此中文版必须以链接形式注明 ...

  5. Intellij IDEA,WebStorm-keymap(转)

    1. ctrl + shift + n: 打开工程中的文件2. ctrl + j: 输出模板3. ctrl + b: 跳到变量申明处4. ctrl + alt + T: 围绕包裹代码(包括zencod ...

  6. 欧拉图 CCF2016第六次 送货

    // 欧拉图 CCF2016第六次 送货 // 思路: // CCF数据很水....这道题有问题 // 先判连通,再dfs边. // 应为输出要满足字典序最小,用vector存图,sort一遍,用st ...

  7. ffmpeg 研究

    ffmpeg -codecs | grep mp3 可以查看ffmpeg是否把mp3编解码模块编译进去了. libmp3lame is the mp3 encoder for ffmpeg. It n ...

  8. 第二百六十八天 how can I坚持

    早上看了个电影<我的少女时代>,挺好看的. 下午从四点玩游戏一直玩到现在,也是疯了. 晚上也没有吃饭,是不是太堕落了. 徐斌他同学今天中午过来,做了个饭,也是服了,好难吃. 还没做好准备, ...

  9. Struts2通配符映射

    1.一个Web 应用可能有成百上千个 action 声明. 可以利用 struts 提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系 2.通配符映射规则 –若找到多个匹配, 没有通配符的 ...

  10. Android实例-全屏显示程序(XE10+小米2)(无图)

    方法一:选中窗体->BorderStyle改为“None” 方法二: 1.下载第三方控件QAndroid.Shell 2.执行FShell.Execute('su -c mount -o rem ...