[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) ...
随机推荐
- 【转】Effective-Objective-C-读书笔记-Item-4-如何正确定义常量 -- 不错
原文网址:http://tutuge.me/2015/03/11/Effective-Objective-C-%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0-Item-4-% ...
- 【转】报错:Program "sh" not found in PATH
原文网址:http://www.cnblogs.com/SadNight/p/3406201.html (1) 报错:Program "sh" not found in PATH ...
- C# 等待另外一个窗体关闭,再进行主线程的代码
方法1 用Form类或其子类的showDialog方法. 比如你在form1里有一个按扭,然后你在Form1的点击事件里写上显示form2的代码: Form2 frm=new Form2(); frm ...
- 问题与解答 [Questions & Answers]
您可以通过发表评论的方式提问题, 我如果有时间就会思考, 并给出答案的链接. 如果您学过Latex, 发表评论的时候请直接输入Latex公式; 反之, 请直接上传图片 (扫描.拍照.mathtype ...
- Linux 多线程编程--线程退出
今天分析项目中进程中虚存一直增长问题,运行10个小时虚存涨到121G ,RSS占用为16G 非常恐怖. Valgrind测试无内存泄漏. 内存32G 64bit系统信息如下: Linux线程使用方式是 ...
- js基础第七天
短信发送验证倒计时案例(常用) 关闭定时器clearInterval this 指向的是 事件的调用者 ,或者是函数的使用者. button 不可以用 disabled 不可用的意思 意思是变成灰色不 ...
- 面向对象基础3(class0523)
怎么实现多态2-接口 接口是定义一种能力,规定子类能干什么和抽象类有些相似,解决类的单根继承.接口可以实现多继承 案例 鸟-麻雀sparrow,鸵鸟ostrich,企鹅penguin,鹦鹉parrot ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- 在C中定义一个动态的二维数组
一般来讲两种办法: 第一种:连续内存分配 #include "stdio.h" #include "stdlib.h" int main() { int x,y ...
- adb logcat调试中常用的命令介绍
Android日志系统提供了记录和查看系统调试信息的功能.日志都是从各种软件和一些系统的缓冲区中记录下来的,缓冲区可以通过 logcat 命 令来查看和使用. adb logcat 命令格式 : ad ...