[Angular-Scaled Web] 9. Control your promises with $q
Learn how to manually control how asynchronous requests are handled with the use of promises. Because $http is built to work with promises, we saw a foreshadow of them in the previous lesson. We will take this a step further but seeing how to manually create a promise and then resolve or reject it as we see fit.
angular.module('eggly.models.categories', [ ])
.service('CategoriesModel', function ($http, $q) {
var CategoriesModel = {},
URLS = {
FETCH: 'data/categories.json'
},
categories; function extract(result) {
return result.data;
} function cacheCategories(result) {
categories = extract(result);
return categories;
} CategoriesModel.getCategories = function() {
return (categories) ? $q.when(categories) : $http.get(URLS.FETCH).then(cacheCategories);
}; CategoriesModel.getCategoryByName = function(categoryName) { function findCategory(){
return _.find(categories, function(c){
return c.name == categoryName;
})
} return $q(function(resolve, reject) {
//resolve it when categories are set
if(categories){
resolve(findCategory());
}else{
//if not set, get the categories
CategoriesModel.getCategories()
.then(function() {
resolve(findCategory());
})
}
})
}; return CategoriesModel;
})
;
[Angular-Scaled Web] 9. Control your promises with $q的更多相关文章
- 混合开发 Hybird Ionic Angular Cordova web 跨平台 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 创建一个web user control
1.创建文件 添加,然后选择web user control 2.添加控件 在工具栏搜索button,然后拖动三个button上去 <%@ Control Language="C#&q ...
- Docs-->.NET-->API reference-->System.Web.UI-->Control-->Methods-->FindControl
https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.control.findcontrol?view=netframework-4.7 ...
- vue,react,angular三大web前端流行框架简单对比
常用的到的网站 vue学习库: https://github.com/vuejs/awesome-vue#carousel (json数据的格式化,提高本地测试的效率) json在线编辑: http: ...
- web api control注册及重写DefaultHttpControllerSelector、ApiControllerActionSelector、ApiControllerActionInvoker
namespace EWorkpal.WebApi { public class HttpNotFoundDefaultHttpControllerSelector : DefaultHttpCont ...
- angular模拟web API
现象:angular Cannot find module 'angular-in-memory-web-api'报错找不动“angular-in-memory-web-api”模块 解决:1.控制台 ...
- web api control注册及重写DefaultHttpControllerSelector、ApiControllerActionSelector、ApiControllerActionInvoker(转)
出处:http://www.cnblogs.com/kingCpp/p/4651154.html namespace EWorkpal.WebApi { public class HttpNotFou ...
- angular form set dynamic control(form动态设置control)
实现效果 form表单控件的实时更新 效果如图 关键代码 validateForm: FormGroup; // 表单校验 constructor( private fb: FormBuilder ) ...
- Angular+ionic2 web端 启动程序出现短暂 白屏或黑屏 的处理小妙招
在ionic2项目启动是会出现短暂的白屏或者黑屏,虽然很短,但是用户体验不太好.上网查了相关的资料,都是针对打包APP的解决办法,针对浏览器端使用的项目没有效果,所以自己写了样式,巧妙的避开这个问题, ...
随机推荐
- [Papers]NSE, $\n u_3$, Lebesgue space, [Pokorny, EJDE, 2003; Zhou, MAA, 2002]
$$\bex \n u_3\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=\frac{3}{2},\quad 2\leq q\leq \i ...
- Unicode转为UTF8
Unicode转换为UTF8 要说这个转换也简单,使用WideCharToMultiByte两次或者直接一次就可以转换. 今天在弄VLC的时候,由于VLC的视频文件名使用UTF8编码,因此当路径中包含 ...
- 初识MFC,WinForm,WPF,Q't
MFC和QT是C++中常见的GUI框架,而WinForm和WPF是C#中常用的框架,不过我们一般很少叫WinForm框架,可能直接叫图形控件类库更多点.反正只是个称呼罢了,爱咋叫就咋叫.另外WinFo ...
- 发布 asp.net网站 到本地IIS
http://blog.csdn.net/jiben2qingshan/article/details/9249139 概述 网站是由一个个页面组成的,是万维网具体的变现形式,关于万维网,网页的方面的 ...
- Lucene学习笔记: 四,Lucene索引过程分析
对于Lucene的索引过程,除了将词(Term)写入倒排表并最终写入Lucene的索引文件外,还包括分词(Analyzer)和合并段(merge segments)的过程,本次不包括这两部分,将在以后 ...
- MAC机常用快捷键整理表格
MAC机常用快捷键整理表格 范围 快捷键 说明 图形 (Command 键)在某些 Apple 键盘上,此键也可能为标志() Control (Control 键) Alt Opt ...
- LinkButton(按钮)
使用$.fn.linkbutton.defaults重写默认值对象. 按钮组件使用超链接按钮创建.它使用一个普通的<a>标签进行展示.它可以同时显示一个图标和文本,或只有图标或文字.按钮的 ...
- 第二百九十四天 how can I 坚持
这是怎么了,好难受,晚上都没吃饭,全身都疼.该咋办. 其实,真的是身体最重要. 洗洗睡了.好难受.
- 第二百四十一天 how can I 坚持
今天去了趟小米之家,红米note3感觉还好吧.小米,希望不会令人失望啊,很看好的,应该不算是米粉吧. 腾讯课堂. hadoop. 摄影. 没有真正的兴趣啊,一心只想着玩,什么事真正的兴趣,就是无时无刻 ...
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...