Basic usage of $q

The http of angularjs is asynchronous and not synchronous. Generally, there is a scenario where the parameters of the asynchronous request are used as conditions to execute the next function. I have been reading other people's blog theories for a long time before I understand it.
http({
	    		method:'post',
	    		url:'aaa',
	    	}).success(function(data){
	    		$scope.flag=true;
	    	});	 
	    	//When the callback function does not succeed, it will not print, of course, you can also put the print in the callback function.
	    	if($scope.flag){
	    		alert("Callback succeeded");
	    	}


Next, look at the use of $q
$scope.call=function(){
	    		var deferred = $q.defer();
		    	http({
		    		method:'post',
		    		url:'aaa',
		    	}).success(function(data){
		    		deferred.resolve('Hello, ' + name + '!');
		    		$scope.flag=true;
		    	});	 
		    	return deferred.promise;
	    	};
	    	//This way you can wait for the callback to succeed before printing.
	    	$scope.call().then(function(){
	    		alert("success");
	    	},function(){
	    		alert("error");
	    	});

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326254932&siteId=291194637