AngularJS中的promise用法分析
本文實例講述了AngularJS中的promise用法。分享給大家供大家參考,具體如下:
JavaScript異步回調(diào)有好處也有壞處,回調(diào)函數(shù)大量嵌套十分復(fù)雜.所以javascript中還有另一種異步處理模式叫promises.在AngularJS中的實現(xiàn)就是$q服務(wù).
下面是一些小例子.
then,catch,finally
在鏈最后的 catch 為整個鏈?zhǔn)教幚硖峁┮粋€異常處理點
在鏈最后的 finally 總是會被執(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第三個參數(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ù)運行 // 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鏈會把上一個 then 的返回結(jié)果傳遞給調(diào)用鏈的下一個 then (如果沒有就是 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 返回一個 promise 對象,下一個 then 只會在這個 promise 被處理結(jié)束的時候調(diào)用。
更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結(jié)》、《AngularJS入門與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對大家AngularJS程序設(shè)計有所幫助。
相關(guān)文章
AngularJS1.X學(xué)習(xí)筆記2-數(shù)據(jù)綁定詳解
本篇文章主要介紹了AngularJS1.X學(xué)習(xí)筆記2-數(shù)據(jù)綁定詳解,具有一定的參考價值,有興趣的可以了解一下。2017-04-04使用AngularJS創(chuàng)建自定義的過濾器的方法
這篇文章主要介紹了使用AngularJS創(chuàng)建自定義的過濾器的方法,AngularJS是非常熱門的JavaScript庫,需要的朋友可以參考下2015-06-06Angularjs在360兼容模式下取數(shù)據(jù)緩存問題的解決辦法
這篇文章主要為大家詳細(xì)介紹了Angularjs在360兼容模式下取數(shù)據(jù)緩存問題的解決辦法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06angular4自定義表單控件[(ngModel)]的實現(xiàn)
這篇文章主要介紹了angular4自定義表單控件[(ngModel)]的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11