routeProvider
In a previous post about testing I mentioned that route resolves can make authoring unit tests for a controller easier. Resolves can also help the user experience.
A resolve is a property you can attach to a route in both ngRoute and the more robust UI router. A resolve contains one or more promises that must resolve successfully before the route will change. This means you can wait for data to become available before showing a view, and simplify the initialization of the model inside a controller because the initial data is given to the controller instead of the controller needing to go out and fetch the data.
As an example, let’s use the following simple service which uses $q to simulate the async work required to fetch some data.
app.factory( "messageService" , function ($q){ return { getMessage: function (){ return $q.when( "Hello World!" ); } }; }); |
And now the routing configuration that will use the service in a resolve.
$routeProvider .when( "/news" , { templateUrl: "newsView.html" , controller: "newsController" , resolve: { message: function (messageService){ return messageService.getMessage(); } } }) |
Resolve is a property on the routing configuration, and each property on resolve can be an injectable function (meaning it can ask for service dependencies). The function should return a promise.
When the promise completes successfully, the resolve property (message in this scenario) is available to inject into a controller function. In other words, all a controller needs to do to grab data gathered during resolve is to ask for the data using the same name as the resolve property (message).
app.controller( "newsController" , function (message) { $scope.message = message; }); |
You can work with multiple resolve properties. As an example, let’s introduce a 2nd service. Unlike the messageService, this service is a little bit slow.
app.factory( "greetingService" , function ($q, $timeout){ return { getGreeting: function (){ var deferred = $q.defer(); $timeout( function (){ deferred.resolve( "Allo!" ); },2000); return deferred.promise; } } }); |
Now the resolve in the routing configuration has two promise producing functions.
.when( "/news" , { templateUrl: "newsView.html" , controller: "newsController" , resolve: { message: function (messageService){ return messageService.getMessage(); }, greeting: function (greetingService){ return greetingService.getGreeting(); } } }) |
And the associated controller can ask for both message and greeting.
app.controller( "newsController" , function ($scope, message, greeting) { $scope.message = message; $scope.greeting = greeting; }); |
Composing Resolve
Although there are benefits to moving code out of a controller, there are also drawbacks to having code inside the route definitions. For controllers that require a complicated setup I like to use a small service dedicated to providing resolve features for a controller. The service relies heavily on promise composition and might look like the following.
app.factory( "newsControllerInitialData" , function (messageService, greetingService, $q) { return function () { var message = messageService.getMessage(); var greeting = greetingService.getGreeting(); return $q.all([message, greeting]).then( function (results){ return { message: results[0], greeting: results[1] }; }); } }); |
Not only is the code inside a service easier to test than the code inside a route definition, but the route definitions are also easier to read.
.when( "/news" , { templateUrl: "newsView.html" , controller: "newsController" , resolve: { initialData: function (newsControllerInitialData){ return newsControllerInitialData(); } } }) |
And the controller is also easy.
app.controller( "newsController" , function ($scope, initialData) { $scope.message = initialData.message; $scope.greeting = initialData.greeting; }); |
One of the keys to all of this working is $q.all, which is a beautiful way to compose promises and run requests in parallel.
routeProvider的更多相关文章
- AgularJS中Unknown provider: $routeProvider解决方案
最近在学习AgularJS, 做到http://angularjs.cn/A00a这一步时发现没有办法执行路由功能, 后来翻看控制台日志,发现提示Unknown provider: $routePro ...
- routeProvider路由的使用
先创建一个主程序文件index.html,内容如下: <!DOCTYPE html> <html ng-app="myApp"> <head> ...
- angularJS $routeProvider
O'Reilly书上的伪代码 var someModule = angular.module('someModule',[...module dependencies]); someModule.co ...
- AngularJS---Unknown provider: $routeProvider
AngularJS路由报错: Unknown provider: $routeProvider 根据先知们的指引,在网上爬贴,有翻到官方的解决文章. 原来在AgularJS1.2.0及其之后的版本中, ...
- 26.angularJS $routeProvider
转自:https://www.cnblogs.com/best/tag/Angular/ O'Reilly书上的伪代码 var someModule = angular.module('someMod ...
- AngularJS实例实战
学习了这么多天的AngularJS,今天想从实战的角度和大家分享一个简单的Demo--用户查询系统,以巩固之前所学知识.功能需求需要满足两点 1.查询所有用户信息,并在前端展示 2.根据id查询用户信 ...
- Nodejs之MEAN栈开发(七)---- 用Angular创建单页应用(下)
上一节我们走通了基本的SPA基础结构,这一节会更彻底的将后端的视图.路由.控制器全部移到前端.篇幅比较长,主要分页面改造.使用AngularUI两大部分以及一些优化路由.使用Angular的其他指令的 ...
- Nodejs之MEAN栈开发(六)---- 用Angular创建单页应用(上)
在上一节中我们学会了如何在页面中添加一个组件以及一些基本的Angular知识,而这一节将用Angular来创建一个单页应用(SPA).这意味着,取代我们之前用Express在服务端运行整个网站逻辑的方 ...
- AngularJs之八
***今天讲一下angularJs的路由功能: 一:angularJs路由. 1.AngularJS 路由允许我们通过不同的 URL 访问不同的内容. 2.通过 AngularJS 可以实现多视图的单 ...
随机推荐
- mysql与mysqld
mysql是客户机/服务器的结构. mysql是客户端行工具,连接mysqld服务,执行sql命令,可认为客户端sdk mysqld 启动mysql数据库服务. 脚本启动mysql服务的命令是 net ...
- linux 通过 ulimit 改善系统性能
https://www.ibm.com/developerworks/cn/linux/l-cn-ulimit/ 概述 系统性能一直是一个受关注的话题,如何通过最简单的设置来实现最有效的性能调优,如何 ...
- Object Pascal 过程与函数
过程与函数 过程与函数是实现一定功能的语句块,是程序中的特定功能单元.可以在程序的其他地方被调用,也可以进行递归调用.过程与函数的区别在于过程没有返回值,而函数有返回值. 1.过程与函数的定义 过程与 ...
- android学习---- WindowManager 接口 (
The interface that apps use to talk to the window manager. 这个接口用于与 window manager (窗口管理器, 应用框架层) 进行交 ...
- HTML5地理位置概述和地理位置对象的详解
一.地理位置 经度 : 南北极的连接线 纬度 : 东西连接的线 二.位置信息从何而来 IP地址 GPS全球定位系统 Wi-Fi无线网络 基站 三.地理位置对象(navi ...
- linux 静态库、共享库
http://blog.chinaunix.net/uid-26833883-id-3219335.html http://blog.chinaunix.net/uid-23069658-id-314 ...
- Android Toolbar样式定制详解
前言 Marterial Design出来也有一段时间了,为了紧跟Google的设计规范,决定在项目中使用Toolbar.使用了一段时间之后,发现很多时候原始的Toolbar并不能满足项目的要求.为了 ...
- win7下载
正式版WIN7的64位旗舰版 http://pan.baidu.com/share/link?shareid=60038&uk=3960800092 下面是正式win8Windows 8 64 ...
- struts2 I18N 国际化
1. 准备properties文件 globalMessages_en_US.properties globalMessages_zh_CN.properties 2. 配置struts.xml &l ...
- solr5.2.1环境搭建教程
环境:w8.1 + solr5.2.1 + apache7.0+jdk1.7 解压:solr5.2.1 复制E:\solr-5.2.1\server\webapps 下的solr.war包到D:\ap ...