路由 Route

为什么用 Route

  • AJAX 请求不会留下 History 记录
  • 用户无法直接通过 URL 进入应用中的指定页面(保存书签、链接分享给朋友)
  • AJAX 对 SEO 是个灾难

    var bookStoreApp = angular.module('bookStoreApp', [
    'ngRoute',
    'ngAnimate',
    'bookStoreCtrls',
    'bookStoreFilters',
    'bookStoreServices',
    'bookStoreDirectives'
    ]); bookStoreApp.config( // 路由机制
    function($routeProvider) {
    $routeProvider.when('/hello', {
    templateUrl: 'tpls/hello.html',
    controller: 'HelloCtrl'
    }).when('/list/:bookId', {
    templateUrl: 'tpls/bookList.html',
    constoller: 'BookListCtrl'
    }).otherwise({
    redirectTo: '/hello'
    });
    }
    );

嵌套 View (Nested Routing for AngularJS)

AngularUI-Router AngularUI

<div ui-view></div>

前端路由的基本原理

  • 哈希 #
  • HTML5 中新的 history API
  • 路由的核心是给应用定义 "状态"
  • 使用路由机制会影响到应用的整体编码方式(需预先定义好状态)
  • 考虑兼容性问题与 "优雅降级"

Angular-UI-Router

ui-sref 指令链接到特定状态

<a ui-sref="home">Home</a>
<a ui-sref="about">About</a>
<a ui-sref="contacts.list">Contacts</a>

$state.includes 返回 true / false

以上方法为查看当前状态是否在某父状态内,比如$state.includes('contacts') 返回 true / false

<!-- 包含在 /contacts 状态内部,即其作为 parant state -->
<li ng-class="{active: $state.includes('contacts')}">
<a ui-serif="contacts.list">Contacts</a>
</li>

ui-sref-active 查看当前激活状态并设置 Class

<li ui-sref-active="active"><a ui-sref="about">About</a></li>

包含模块

angular.module('uiRouter', ['ui.router']);

方便获得当前状态的方法,绑到根作用域

app.run(['$rootScope', '$state', '$stateParams',
function($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
}
]);

路由重定向 $urlRouterProvider

app.config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$urlRouterProvider
// 错误的路由重定向
.when('/c?id', '/contacts/:id')
.when('/user/:id', '/contacts/:id')
.otherwise('/');
}
]);

状态配置

$stateProvider.
state('about', {
url: '/about',
template: '<h1>Welcome to UI-Router Demo</h1>', // optional below
templateProvider: ['$timeout',
function($timeout) {
return $timeout(function() {
return '<p class="lead">UI-Router Resource</p>' +
'<p>The second line</p>'
}, 100);
}
], templateUrl: 'about.html', templateUrl: function() {
return 'about.html';
}, controller: 'UIRouterCtrl', // below is a state controller picked from UI-Router official sample
controller: ['$scope', '$state', 'contacts', 'utils',
function ($scope, $state, contacts, utils) {
$scope.contacts = contacts; $scope.goToRandom = function () {
var randId = utils.newRandomKey($scope.contacts, 'id', $state.params.contactId); $state.go('contacts.details', { contactId: randId });
}
}
]
});

嵌套配置 Configure

JavaScript Codes

Parent Router

$stateProvider.
.state('contacts', { // abstract: true 表明此状态不能被显性激活,只能被子状态隐性激活
abstract: true,
url: '/contacts',
templateUrl: 'contacts.html', // resolve 被使用来处理异步数据调用,以下是返回一个 promise -> contacts.all()
resolve: {
contacts: ['contacts',
function(contacts) {
// 以下方法被放在 contacts.service.js 中,以 factory 存在
return contacts.all();
}
]
}, // below is a state controller picked from UI-Router official sample
controller: ['$scope', '$state', 'contacts', 'utils',
function ($scope, $state, contacts, utils) {
$scope.contacts = contacts; $scope.goToRandom = function () {
var randId = utils.newRandomKey($scope.contacts, 'id', $state.params.contactId); $state.go('contacts.details', { contactId: randId });
}
}
]
})

Children Router (Nested Router)

$stateProvider
.state('contacts.list', {
url: '',
templateUrl: 'contacts.list.html'
}) .state('contacts.detail', {
url: '/{contactId:[0-9]{1,4}}', // view 用在该状态下有多个 ui-view 的情况,可以对不同的 ui-view 使用特定的 template, controller, resolve data
// 绝对 view 使用 '@' 符号来区别,比如 'foo@bar' 表明名为 'foo' 的 ui-view 使用了 'bar' 状态的模板(template),相对 view 则无
views: {
// 无名 view
'': {
templateUrl: 'contacts.detail.html',
controller: ['$scope', '$stateParams', 'utils',
function ($scope, $stateParams, utils) {
$scope.contact = utils.findById($scope.contacts, $stateParams.contactId);
}
]
}, // for "ui-view='hint'"
'hint@': {
template: 'This is contacts.defail populating the "hint" ui-view'
}, // for "ui-view='menuTip'"
'menuTip': {
templateProvider: ['$stateParams',
function($stateParams) {
return '<hr><small class="muted">Contact ID: ' + $stateParams.contactId + '</small>';
}
]
}
}
})

HTML Codes

<h2>All Contacts</h2>
<ul>
<li ng-repeat="contact in contacts">
<a ui-sref="contacts.detail({contactId:contact.id})">{{contact.name}}</a>
</li>
</ul>
 

Angular-UI-Router的更多相关文章

  1. angular ui.router 路由传参数

    angular已经用了一段时间了,最近在做路由,做一下笔记. 路由跳转的时候进行穿参 ui.router方式 <a ui-sref="edit({id:5})"> 编辑 ...

  2. 规范 : angular ui router path & params

    在seo文章中提到url的path 必须是 why-us,而不是whyUS 所以定了规范,所有的path 必须why-us path ?后尾的是用来filter的,所以可以WhyUs 如果是不需要给s ...

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

    Angular UI Router: Different states with same URL? The landing page of my app has two states: home-p ...

  4. angular : $location & $state(UI router)的关系

    次序:angular 的 location会先跑 $rootScope.$on("$locationChangeStart", function (scope, newUrl, o ...

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

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

  6. angularjs ngRoute和ui.router对比

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

  7. ngRoute 与ui.router区别

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

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

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

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

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

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

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

随机推荐

  1. 【v2.x OGE教程 20】粒子效果

    1.介绍 粒子系统表示三维计算机图形学中模拟一些特定的模糊现象的技术.而这些现象用其他传统的渲染技术难以实现的真实感的 game physics.常常使用粒子系统模拟的现象有火.爆炸.烟.水流.火花. ...

  2. Android系统的开机画面显示过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/7691321 好几个月都没有更新过博客了,从今天 ...

  3. php错误日志级别

    ; E_ALL             所有错误和警告(除E_STRICT外) ; E_ERROR           致命的错误.脚本的执行被暂停. ; E_RECOVERABLE_ERROR   ...

  4. js 创建html元素 渲染html元素

    var p1 = document.getElementById('p1'); //添加的元素类型及属性var newNode = document.createElement("input ...

  5. CMD下修改IP地址

    @echo off netsh interface ip set address name="本地连接" static 192.168.1.55 255.255.255.0 192 ...

  6. Entity Framework 6 Code First创建

    基本上我是DB先设计好的,所以就按现存在的table去写程式. 1.Web.config里配置Db连接字串,Connection String Name为DefaultConnection <c ...

  7. jsp include 乱码问题的解决

    jsp include 乱码问题的解决 博客分类: Java JSPWeb浏览器IESpring  jsp include 乱码问题的解决 jsp include 的文件有时候会出现乱码,经过测试发现 ...

  8. Emgu学习笔记(一)安装及运行Sample

    1.简单说明 Emgu是Dot Net平台对OpenCV的封装,本质上没有增加新功能,是通过Dot Net的平台调用技术直接调用OpenCV C++语言写的库,使用我们可以方便用.net平台通过Ope ...

  9. 在vs.net c#中添加mysql模型

    http://weblogs.asp.net/gunnarpeipman/getting-mysql-work-with-entity-framework-4-0 http://dev.mysql.c ...

  10. (转+原)VC编译错误:uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) 已经在 LIBCMT.lib(new.obj) 中定义

    参考网址:http://zhanyonhu.blog.163.com/blog/static/16186044201023094754832/ 1>uafxcw.lib(afxmem.obj) ...