AngularJS中的promise用法分析
本文實(shí)例講述了AngularJS中的promise用法。分享給大家供大家參考,具體如下:
JavaScript異步回調(diào)有好處也有壞處,回調(diào)函數(shù)大量嵌套十分復(fù)雜.所以javascript中還有另一種異步處理模式叫promises.在AngularJS中的實(shí)現(xiàn)就是$q服務(wù).
下面是一些小例子.
then,catch,finally
在鏈最后的 catch 為整個(gè)鏈?zhǔn)教幚硖峁┮粋€(gè)異常處理點(diǎn)
在鏈最后的 finally 總是會(huì)被執(zhí)行,不管 promise 被處理或者被拒絕,起清理作用
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="bootstrap.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<script src="jQuery.min.js"></script>
<script src="angular.min.js"></script>
<script src="bootstrap.min.js"></script>
<script type="text/javascript">
var myapp = angular.module('myapp', []);
myapp.controller('myController', function($scope, $q) {
$scope.send = function() {
var deferred = $q.defer();
var promise = deferred.promise;
promise
.then(function() {
console.log('resolve.....')
}, function() {
console.log('reject.....');
}, function() {
console.log('notify.....');
})
.catch(function() {
console.log('catch..error..')
})
.finally(function() {
console.log('anywhere will be called..');
});
deferred.reject('resolve');
};
});
</script>
<style type="text/css">
</style>
</head>
<body ng-app="myapp">
<div ng-controller="myController">
<button class="btn" ng-click="send()">click</button>
</div>
</body>
</html>
then第三個(gè)參數(shù)(表征狀態(tài))的應(yīng)用
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="bootstrap.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<script src="jquery.min.js"></script>
<script src="angular.min.js"></script>
<script src="bootstrap.min.js"></script>
<script type="text/javascript">
var myapp = angular.module('myapp', []);
myapp.controller('myController', function($scope, dataService) {
$scope.send = function() {
dataService.getData()
.then(function success(data) {
console.log(data);
}, function error(error) {
console.log(error);
}, function status(process) {
console.log(process);
});
};
});
myapp.factory('dataService', function($q, $interval) {
return {
getData : function() {
var deferred = $q.defer();
var process = 0;
var interval = $interval(function() {
process += 10;
deferred.notify(process);
//reject之后不再繼續(xù)運(yùn)行
// if (process == 50) {
// deferred.reject(process);
// }
if (process >= 100) {
$interval.cancel(interval);
deferred.resolve(process);
}
}, 1000);
return deferred.promise;
}
};
});
</script>
<style type="text/css">
</style>
</head>
<body ng-app="myapp">
<div ng-controller="myController">
<button class="btn" ng-click="send()">click</button>
</div>
</body>
</html>
then鏈?zhǔn)?/p>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="bootstrap.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<script src="jquery.min.js"></script>
<script src="angular.min.js"></script>
<script src="bootstrap.min.js"></script>
<script type="text/javascript">
var myapp = angular.module('myapp', []);
myapp.controller('myController', function($scope, $q) {
$scope.send = function() {
var deferred = $q.defer();
var promise = deferred.promise;
promise
.then(function() {
console.log('1.....')
})
.then(function() {
console.log('2....');
});
deferred.resolve('resolve');
};
});
</script>
<style type="text/css">
</style>
</head>
<body ng-app="myapp">
<div ng-controller="myController">
<button class="btn" ng-click="send()">click</button>
</div>
</body>
</html>
then鏈會(huì)把上一個(gè) then 的返回結(jié)果傳遞給調(diào)用鏈的下一個(gè) then (如果沒(méi)有就是 undefined).
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="bootstrap.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<script src="jquery.min.js"></script>
<script src="angular.min.js"></script>
<script src="bootstrap.min.js"></script>
<script type="text/javascript">
var myapp = angular.module('myapp', []);
myapp.controller('myController', function($scope, $q, $timeout) {
$scope.send = function() {
var deferred = $q.defer();
var promise = deferred.promise;
deferred.resolve('resolve');
promise
.then(function(data) {
console.log(data);
var _deferred = $q.defer();
$timeout(function() {
_deferred.resolve('resolve_');
}, 1000);
return _deferred.promise;
})
.then(function(data) {
console.log(data);
});
};
});
</script>
<style type="text/css">
</style>
</head>
<body ng-app="myapp">
<div ng-controller="myController">
<button class="btn" ng-click="send()">click</button>
</div>
</body>
</html>
如果 then 返回一個(gè) promise 對(duì)象,下一個(gè) then 只會(huì)在這個(gè) promise 被處理結(jié)束的時(shí)候調(diào)用。
更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結(jié)》、《AngularJS入門與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對(duì)大家AngularJS程序設(shè)計(jì)有所幫助。
相關(guān)文章
AngularJs實(shí)現(xiàn)聊天列表實(shí)時(shí)刷新功能
這篇文章主要介紹了AngularJs實(shí)現(xiàn)聊天列表實(shí)時(shí)刷新功能,需要的朋友可以參考下2017-06-06
AngularJS 模型詳細(xì)介紹及實(shí)例代碼
本文主要介紹 AngularJS模型,這里詳細(xì)介紹了AngularJS 模型中的知識(shí)點(diǎn),并提供實(shí)例代碼,有需要的小伙伴可以參考下2016-07-07
AngularJS+bootstrap實(shí)現(xiàn)動(dòng)態(tài)選擇商品功能示例
這篇文章主要介紹了AngularJS+bootstrap實(shí)現(xiàn)動(dòng)態(tài)選擇商品功能,涉及AngularJS指令、事件響應(yīng)及頁(yè)面元素動(dòng)態(tài)操作相關(guān)技巧,需要的朋友可以參考下2017-05-05
AngularJS封裝$http.post()實(shí)例詳解
這篇文章主要介紹了 AngularJS封裝$http.post()實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Bootstrap + AngularJS 實(shí)現(xiàn)簡(jiǎn)單的數(shù)據(jù)過(guò)濾字符查找功能
這篇文章主要介紹了 Bootstrap + AngularJS 實(shí)現(xiàn)簡(jiǎn)單的數(shù)據(jù)過(guò)濾字符查找功能,代碼簡(jiǎn)單易懂,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2017-07-07
AngularJS使用ngOption實(shí)現(xiàn)下拉列表的實(shí)例代碼
這篇文章主要介紹了AngularJS使用ngOption實(shí)現(xiàn)下拉列表的實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-01-01
angularJs提交文本框數(shù)據(jù)到后臺(tái)的方法
今天小編就為大家分享一篇angularJs提交文本框數(shù)據(jù)到后臺(tái)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10

