http({ method:'post', url:'aaa', }).success(function(data){ $scope.flag=true; }); //当回调函数没有成功时, 不会打印,当然也可以把打印放在回调函数中。 if($scope.flag){ alert("回调成功"); }
接下来看$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; }; //这样就可以等回调成功后才会打印。 $scope.call().then(function(){ alert("success"); },function(){ alert("error"); });