-4
$http({
    url: "php/InsertTab.php",
        method: "POST",
        data: {
            'userId': userId,
        },
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function(data, status, headers, config) {

        myVar = data;

        }).error(function(data, status, headers, config) {
    });

how to wait success scope done loading? there's no .done in angularjs's $http

2 Answers2

2

You can use .success method:

.success(function(data, status, headers, config) {

  // this callback will be called asynchronously
  // when the response is available

})
Community
  • 1
  • 1
Diana Nassar
  • 2,303
  • 14
  • 17
0

If you mean updating scope, you can directly assign to scope from the success callback.

$http({
    url: "php/InsertTab.php",
        method: "POST",
        data: {
            'userId': userId,
        },
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function(data, status, headers, config) {

        $scope.myVar = data;

        }).error(function(data, status, headers, config) {
    });

If you need to show a loading indicator or so, you can use the promise API, which provides a then(success, error) method, like that:

function MyCtrl($scope) {
    $scope.ajaxLoading = true;

    $scope.ajaxComplete = function() {
        $scope.ajaxLoading = false;
    };

    $http({
        url: "php/InsertTab.php",
            method: "POST",
            data: {
                'userId': userId,
            },
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).success(function(data, status, headers, config) {

            $scope.myVar = data;

            }).error(function(data, status, headers, config) {
        }).then($scope.ajaxComplete, $scope.ajaxComplete);
}

You can later make this generic using AJAX interceptors. See the AJAX documentation page:

https://docs.angularjs.org/api/ng/service/$http

Meligy
  • 35,654
  • 11
  • 85
  • 109