我聽說$ q和promise對於同步編程非常有用。爲什麼我的承諾中的函數沒有運行
我希望我的第二個函數能夠在我的第一個有超時的函數之後運行。所以基本上我想MT第一個函數來完成我的secondfunction工作
我的代碼之前先運行是:
<head>
<link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl">
<button ng-click="myClick()">Click Me!</button>
</div>
<script type="text/javascript">
angular.module('myApp', [])
.controller('myCtrl',['$scope', '$timeout', '$q', function($scope, $timeout, $q){
$scope.functionOne = function(){
return $q(function(resolve, reject){
$timeout(function(){
alert("dean");
}, 3000);
})
};
$scope.functionTwo = function(){
alert("armada");
}
$scope.myClick = function(){
var promise = $scope.functionOne();
promise.then(function(){
$scope.functionTwo();
}, function(){
alert("fail");
})
};
}]);
</script>
</body>
Plunker:https://plnkr.co/edit/6hJF4mxrCQ17XXA43eUl?p=preview
因爲你不叫'resolve'或'reject'? – Icepickle
是的,正確答案已經給出 –