Search code examples
jqueryangularjsjquery-ajaxq

jQuery Post , then Access AngularJS $scope


In our AngularJS app, we need to use jQuery to AJAX POST data to a back end. We can't use $http as it doesn't format the object in a way our back end needs it. jQuery does.

I am trying to set a $scope variable once the jQuery POST is done, but can't access $scope.

 $scope.submit = function () {
        $scope.isSubmitting = true;
        $.post("DoStuff",{ createmodel: $scope.model })
            .then(function(result) {
                ///THIS FAILS TO DO ANYTHING
                $scope.isSubmitting = false;
            });

    };

How do I access $scope in the jQuery POST after it comes back.


Solution

  • It fails to do anything because jQuery does not inform Angular that a scope variable has changed. You can simply achieve this by adding $scope.$apply()

    In your case the solution is

    $scope.isSubmitting = false;
    $scope.$apply();