[AngularJS] Best Practise - Resolve promises in router, defer controllers
See more:http://toddmotto.com/opinionated-angular-js-styleguide-for-teams/
/**
* Created by Answer1215 on 1/13/2015.
*/ /**
* Controller(s)
* */ function AppController($rootScope, $location) { var appCtrl = this; $rootScope.$on('$routeChangeError', function(event, current, previous, rejection) {
appCtrl.message = rejection.message;
appCtrl.isError = true;
}) appCtrl.goHome = function() { $location.path('/');
}
} /**
* Directive(s)
* */ function ErrorDirective() {
return {
restrict: "EA",
bindToController: true,
controller: 'AppController',
controllerAs: 'appCtrl',
templateUrl: 'error.html'
}
} /**
* Angular module
* */ angular.module('app', [
'ngRoute',
'app.home'
])
.constant('PEOPLE_URL', 'data.json')
.controller('AppController', AppController)
.directive('error', ErrorDirective)
.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: "home.html",
controller: "HomeController",
controllerAs: "homeCtrl",
/*
* resolve run first, prepare the data ready,
* then controller & template show up
* */
resolve: HomeController.resolve
});
});
/**
* Created by Answer1215 on 1/13/2015.
*/ function HomeController(loadPeople, getMessage) {
var homeCtrl = this; homeCtrl.message = getMessage;
homeCtrl.people = loadPeople;
} HomeController.resolve = { loadPeople: function(SimpleDataService) {
return SimpleDataService.getPeople();
},
getMessage : function(SimpleDataService) {
return SimpleDataService.message;
}
} function SimpleDataService($http, $q, PEOPLE_URL) {
var simpleData = {};
simpleData.people = [];
simpleData.message = "Hello From Service"; simpleData.getPeople = function() {
return fetchPeople().then(function(result) {
return result.data;
}); } function fetchPeople() {
var defer = $q.defer();
$http.get(PEOPLE_URL)
.success(function(data, status, headers, config) {
simpleData.people = data;
defer.resolve({data: data, message: "OK"});
}).
error(function(data, status, headers, config) { if(status === 404){
data = data;
defer.reject({data: data, message: "Cannot find " + config.url});
}
});
return defer.promise;
} return simpleData;
} angular.module('app.home', [])
.controller('HomeController', HomeController)
.service('SimpleDataService', SimpleDataService);
While the routes are being resolved we want to show the user something to indicate progress. Angular will fire the $routeChangeStart event as we navigate away from the page, which we can show some form of loading and ajax spinner, which can then be removed on the $routeChangeSuccess
[AngularJS] Best Practise - Resolve promises in router, defer controllers的更多相关文章
- AngularJS ui-router 用resolve、service预先加载数据写法,属于优化性能方面吧
		
AngularJS的service怎么声明此处就不再赘述,下面的例子是ui-router中使用service的实现代码 $stateProvider.state('myState', { url: & ...
 - [AngularJS] Best Practise - Controller
		
ControllerAs: Use thecontrollerAs syntax always as it aids in nested scoping and controller instance ...
 - [AngularJS] Best Practise - Minification and annotation
		
Annotation Order: It's considered good practice to dependency inject Angular's providers in before o ...
 - [AngularJS] Best Practise - Module
		
Module definitions Angular modules can be declared in various ways, either stored in a variable or u ...
 - 转: angular编码风格指南
		
After reading Google's AngularJS guidelines, I felt they were a little too incomplete and also guide ...
 - AngularJS之高级Route【三】(八)
		
前言 我们知道默认的路由提供(Route Provider)在复杂的应用程序中是不太适合应用场景,它存在诸多限制,所以在Angular 1.2之后此时我们不得不将路由提供作为一个单独的模块当我们需要使 ...
 - angularJS中的Promise对象($q)的深入理解
		
原文链接:a better way to learn AngularJS - promises AngularJS通过内置的$q服务提供Promise编程模式.通过将异步函数注册到promise对象, ...
 - Promise in AngularJS
		
What's promise Angular’s event system provides a lot of power to our Angular apps. One of the most p ...
 - 转AngularJS路由插件
		
AngularJS学习笔记--002--Angular JS路由插件ui.router源码解析 标签: angular源码angularjs 2016-05-04 13:14 916人阅读 评论(0) ...
 
随机推荐
- php类的实现
			
zend_class_entry typedef struct _zend_class_entry zend_class_entry; struct _zend_class_entry { char ...
 - bzoj1150: [CTSC2007]数据备份Backup
			
题目大意: 在n个点中,选出k对相邻的互不相同的点,使k段距离的总和最小. 贪心,双向链表. 首先,点之间的距离是动态的,所以要用堆来维护. 每次都选择最近的点.但因为其他情况,可能最终不会选择这 ...
 - NOI2002robot
			
这题又是纯数论题…… 独立数就是欧拉函数,政客和军人的含义已经说的很清楚了,学者是最多的…… 首先,如果我们知道了政客和军人的答案,那就只要用n的所有因子的欧拉函数值减去这两个值,然后取模就行了. 但 ...
 - NOI2002银河英雄传说
			
原先就看过这道题,觉得很复杂. 不知道为什么今天一看觉得好水啊…… 难道这就是并查集的启发式合并? 数组d[i]表示i到其父节点的距离,即中间隔了多少船舰. 数组sum[i]记录以i为根的集合总共有多 ...
 - Java [Leetcode 42]Trapping Rain Water
			
题目描述: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...
 - Java [Leetcode 39]Combination Sum
			
题目描述: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in ...
 - linux下socket keep alive讲解
			
[需求] 不影响服务器处理的前提下,检测客户端程序是否被强制终了.[现状]服务器端和客户端的Socket都设定了keepalive属性.服务器端设定了探测次数等参数,客户端.服务器只是打开了keepa ...
 - POJ 2001-Shortest Prefixes(Trie 入门)
			
题意:给你一组串,求每个串在组中唯一标志的最短前缀 分析:保存树上经过各节点的单词个数,扫描每个串当经过节点单词个数是一(能唯一标志)结束 #include <map> #include ...
 - 《Python基础教程(第二版)》学习笔记 -> 第四章 字典
			
字典是Python中唯一内建的映射类型. 字典中的值并没有特殊的顺序,但是都存储在一个特定的键(Key)里.键可以是数字.字符串甚至是元组. 字典的使用 某些情况下,字典比列表更加适用: 表征游戏棋盘 ...
 - error: Setup script exited with error: Unable to find vcvarsall.bat
			
安装mxnet python版本时遇到“Unable to find vcvarsall.bat”错误搜索一下后查到如下方法: python 3.5.2版本依赖高版本的vs解决办法是安装vs2015的 ...