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 可以实现多视图的单 ...
随机推荐
- Java实现分页数据获取CachedRowSet
步骤 1.加载驱动 2.连接数据库 3.创建ResultSet 4.创建CacheRowSet 5.设置并获取分页数据 6.执行查询,展示数据 package ch13; import javax.s ...
- iOS 开发之 Xcode6 创建真机调试证书
http://jingyan.baidu.com/article/ff411625b8141312e48237a7.html 1.登录苹果开发者中心 2.登录后的界面如图所示,如果没有最上面的两个选项 ...
- maven实战_01_搭建maven开发环境
一 下载maven 在maven官网上可下载maven:http://maven.apache.org/download.cgi 下载好后,解压.我的解压到了:D:\maven\apache-mave ...
- Python标准库06 子进程 (subprocess包)
这里的内容以Linux进程基础和Linux文本流为基础.subprocess包主要功能是执行外部的命令和程序.比如说,我需要使用wget下载文件.我在Python中调用wget程序.从这个意义上来说, ...
- python操作mongodb之基础操作
#coding:utf-8 __author__ = 'hdfs' import pymongo from pymongo import MongoClient client = MongoClien ...
- eclipse设置字体、背景(豆绿)色、自动提示
背景色:(护眼豆绿色) window-->preferences-->General-->Editors-->Text Editors-->(最下遍一栏中的)Backgr ...
- java 多线程8(join)
join(): 加入.一个线程如果执行了join语句,那么就有新的线程加入,执行该语ij 句的线程必须要让步给新加入的线程先完成任务,然后才能继续执行. import java.util.Scann ...
- linux笔记:软件包管理-软件包简介
软件包分类: 1.源码包: 2.二进制包(在centos里就是RPM包.和源码包的区别在于已经经过编译.) 源码包的优点和缺点: RPM包的优点和缺点:
- RESEACH PAPER
个,proquest的username和password赫然在目,别急,再看第4个结 果"HB Thompson Subscription Online Databases", ...
- sass less
CSS 预处理器技术已经非常的成熟,而且也涌现出了越来越多的 CSS 的预处理器框架.本文向你介绍使用最为普遍的三款 CSS 预处理器框架,分别是 Sass.Less CSS.Stylus. 首先我们 ...