[AngularJS] Introduction to ui-router

Introduce to basic $stateProvider.state() with $stateParams services. Understand how nested router works.

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

时间: 2024-07-30 03:26:07

[AngularJS] Introduction to ui-router的相关文章

AngularJS学习之 ui router

1.安装 bower install --save angular_ui-router 2.在项目主页面 index.html中添加 <div ui-view=""></ui-view>/*login页面,dashboard.html将会嵌入此处*/ 3.在app.js页面设置 .config(function ($stateProvider,$urlRouterProvider) { $urlRouterProvider.otherwise('/login')

Angularjs中UI Router全攻略

摘自:Angularjs中UI Router全攻略 温馨提示:想要了解 angular-ui-router的同学,从上往下读一遍,能带随着coding那就更好了,保证你对angular-ui-router基本全部掌握. 如何引用依赖angular-ui-router angular.module('app',["ui.router"]) .config(function($stateProvider){ $stateProvider.state(stateName, stateCofi

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

本文转自:http://www.oschina.net/translate/angularjs-multi-step-form-using-ui-router 今天我们将使用AngularJs和伟大的UI Router以及Angular ngAnimate module创建一个带动画的多步表单.这项技术可以用在你想要简化用户操作的大表单上. 我们看到这项技术已经应用在了许多的网页上.比如购物车,注册表单,入职流程以及许多多步表单,让用户更容易在线填写表单. 下面我们将构建它: 使用UI Rout

Angularjs中UI Router用法小记录

今天自己参考已有的项目代码学习了下UI Router的用法,写了个小demo,验证了下自己的想法,现把使用情况记录一下. 1.入口文件index.html,引入项目所需的js文件,标注ng-app,创建ui-view元素,为后面的嵌套做容器准备. <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <meta charset="UTF-8">

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

在单页面应用中要把各个分散的视图给组织起来是通过路由机制来实现的.本文主要对 AngularJS 原生的 ngRoute 路由模块和第三方路由模块 ui.router 的用法进行简单介绍,并做一个对比. ngRoute 使用方法 1) 引入 angular-route lib 无论是 ngRoute 还是 ui.router ,作为框架额外的附加功能,都必须以 模块依赖 的形式被引入. 1 <script src="lib/angular-route.js"></sc

angular : $location &amp; $state(UI router)的关系

次序:angular 的 location会先跑 $rootScope.$on("$locationChangeStart", function (scope, newUrl, oldUrl) { log("$locationChangeStart from " + oldUrl) log("$locationChangeStart to " + newUrl) }); 接着是 ui router $state $rootScope.$on(&q

angular 的ui.router 定义不同的state 对应相同的url

Angular UI Router: Different states with same URL? The landing page of my app has two states: home-public, home-logged-in. Now I want to show both states on the same URL, but let the controller and template depend on the user session (is the user log

Angularjs ui router,路由嵌套 父controller执行问题

路由路径设置:structured.text   :structured是第一层路由,text是第二层路由: 问题如下,当$state.transitionTo路由到第二层是,structured的controller也会执行一次,导致页面控件重新加载刷新. $state.transitionTo( "structured.text", { args : $stateParams }, { reload: false} ); 查了github看到 https://github.com/

AngularJS学习 之 UI以及逻辑生成

学习<Angular高级编程>理解如下 要求: 创建如下界面,有导航栏,一个Watchlists面板,面板上有个加号button,一句说明“”Use+to create a list“” 点击 + 会弹出如下窗口 输入一个name (比如:医疗)一个description(医疗股票监视), Create按钮就会高亮,点击create后,就会显示如下样式 实现 1.UI 也就是html以及css的实现 当然是到app/view/目录下创建Html文件啦,因为这两个页面的形式 在后面的设计中会经常