<!DOCTYPE html>
<html ng-app="app">
<head lang="en">
<meta charset="UTF-8">
<title>new $q constructor </title>
<script src="bower_components/angular/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl as vm">
<button ng-click="vm.fire(true)">Reject it</button>
<button ng-click="vm.fire(false)">Resolve it</button>
<h3>Last Resolved Value</h3>
<pre>{{vm.resolveData | json}}</pre> <h3>Last Rejected Value</h3>
<pre>{{vm.rejectData | json}}</pre>
</body>
</html>
/**
* Created by Answer1215 on 11/13/2014.
*/
function MainCtrl($q){
'use strict';
var vm = this;
vm.fire = fire;
vm.resolveData = null;
vm.rejectData = null; function fire(rejectIt){
console.log('fire');
doAsync(rejectIt).then(function(data){
vm.resolveData = data;
}, function(error){
vm.rejectData = error;
});
} //$q usually sepreate from the logic
function doAsync(rejectIt){
return $q(function(resolve, reject){
setTimeout(function(){
var doneTime = +new Date();
console.log(doneTime);
if(!rejectIt){
resolve({
resolveData: 'resolve it at '+doneTime
});
}else{
reject({
rejectData: 'reject it at '+doneTime
});
}
}, 500);
});
}
} angular.module('app',[])
.controller('MainCtrl', MainCtrl);

Read More: http://jsbin.com/jetoyo/4/edit?html,js,output

[AngularJS] Angular 1.3 new $q constructor的更多相关文章

  1. angularJS中的Promise对象($q)的深入理解

    原文链接:a better way to learn AngularJS - promises AngularJS通过内置的$q服务提供Promise编程模式.通过将异步函数注册到promise对象, ...

  2. angularjs系列之轻松使用$q进行异步编程

    第一部分关于js中的异步编程 异步编程简单的说就是你写了一段代码,但他不会按照你书写代码的顺序立即执行,而是等到程序中发生了某个事件(如用户点击了某个按钮,某个ajax请求得到了响应)才去执行这段代码 ...

  3. angular的异步处理$q的使用(promise)

    Angular中的promise: Promise是一种异步方式处理值的方法.代表了一个函数最 终可能的返回值或者抛出的异常 在之前,通常都是使用闭包或者回调来响应非同步的有意义数据 使用promis ...

  4. AngularJs 与服务器通信 $http, $q, $resource

    $http服务是AngularJS系统自带的,可以用来进行网络通信.获取远程服务器的数据.要记住的是,$http是对浏览器XMLHttpRequest的封装,也就是说,它其实是Ajax. $http( ...

  5. AngularJs angular.Module模块接口配置

    angular.Module Angular模块配置接口. 方法: provider(name,providerType); name:服务名称. providerType:创建一个服务的实例的构造函 ...

  6. [Angularjs]angular ng-repeat与js特效加载先后导致的问题

    写在前面 最近在项目中遇到这样的一个前端的bug,在ng-repeat中绑定的图片,有一个晃动的特效,在手机端浏览的时候,图片有时候会正常展示,有时就展示不出来.当时猜测是因为angularjs与特效 ...

  7. [AngularJS] Angular 1.5 $transclude with named slot

    In Angular 1.5, there is no link and compile. So use if you transclude, you cannot access the fifth ...

  8. [AngularJS] Angular 1.3 ngMessages with ngAnimate

    Note: Can use $dirty to check whether user has intracted with the form: https://docs.angularjs.org/a ...

  9. [AngularJS] Angular 1.3 $submitted for Form in Angular

    AngularJS 1.3 add $submitted for form, so you can use  $submitted  to track whether the submit event ...

随机推荐

  1. Wannafly挑战赛17 B

    题解 大概就是求证这个 \[\sum_i^nC_{n}^i*C_n^i = C_{2n}^n\] 证明: \[(1+x)^{2n} = [C(0,n)+C(1,n)*x+...+C(n,n)*x^n] ...

  2. 我的OI生涯 第四章

    第四章 晚上来机房的人越来越多了,我也注意到一个常年独自坐在一个角落的男人————郝哥. 郝哥为人很安静,只是那时我还不知道他好不好,就没有与他交流过什么,这个优秀的男人以后我们还会提到,这里先不讲. ...

  3. 【MPI】并行求和

    比较简单的并行求和 读入还是串行的 而且无法处理线程数无法整除数据总长度的情况 主要用到了MPI_Bcast MPI_Scatter MPI_Reduce typedef long long __in ...

  4. python3-开发进阶Flask的基础(3)

    上篇我们大概简单描述了一下上下文管理,这篇来具体来说说, 上下管理的request 上下管理的session 第三方组件:flask-session pymysql操作数据库  数据库连接池 一.前奏 ...

  5. [转]Intent和IntentFilter详解

        Intent   Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描 ...

  6. CROC 2016 - Qualification C. Hostname Aliases map

    C. Hostname Aliases 题目连接: http://www.codeforces.com/contest/644/problem/C Description There are some ...

  7. 阻止a标签跳转

       一.在html中 1. <a href="javascript:;"></a> 2. <a href="###">&l ...

  8. wpf简单的绘图板

    xaml: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft ...

  9. delphi tcp/ip IdTCPServer1实例一

    unit Unit1; interface uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Form ...

  10. SQLiteOpenHelper 操作不成功

    SDK和ADT为22.6.2版本号 project为4.4.2 今天在练习SQLiteOpenHelper里,使用的是三个JAVA文件操作.DatabaseHelper.java,Const.java ...