0

I have an angular app where i am trying to post some elements into a database. Here i am using a modal where i check some courses such that the checked courses will be posted using the $http.post Here is the function to post the data:

$scope.ok = function () {
              //$scope.subcategory.subcategory2 = [];
                for(var i = 0; i < $scope.selectedCourses.length; i++){
                    debugger
                  //$scope.subcategory.subcategory2.push({course: $scope.selectedCourses[i], term:"--",credit:"--",grade:"--"});
                  var variablesToSend = {
                                        "student": 2773951,
                                        "course_name": $scope.selectedCourses[i].course.course_name,
                                        "title": $scope.selectedCourses[i].course.title,
                                        "credit": 3,
                                        }
                    $http.post('/api/studentplannedcredit/', variablesToSend).then(function(response){
                        console.log(response);
                        //alert('post added');
                        //document.location.reload(true);

                    }, function(response){
                        console.log(response);
                        alert('Unable to add Course');
                    });             
                }
                $modalInstance.close();
            };

The error i am getting here is, when i calling the function, it gives me the following error:

Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.2.8/$rootScope/inprog?p0=%24digest
    at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:78:12
    at beginPhase (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:12262:15)
    at Scope.$get.Scope.$apply (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:12048:11)
    at Scope.$delegate.__proto__.$apply (<anonymous>:855:30)
    at HTMLDivElement.<anonymous> (http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.min.js:8:20385)
    at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:2612:10
    at forEach (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:309:20)
    at HTMLDivElement.eventHandler (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:2611:5)
    at http://127.0.0.1:8000/static/js/app.js:161:7
    at deferred.promise.then.wrappedErrback (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:10952:78) angular.js:9413
(anonymous function) angular.js:9413
$get angular.js:6832
$get.Scope.$apply angular.js:12051
$delegate.__proto__.$apply VM437152:855
(anonymous function) ui-bootstrap-tpls-0.10.0.min.js:8
(anonymous function) angular.js:2612
forEach angular.js:309
eventHandler angular.js:2611
(anonymous function) app.js:161
deferred.promise.then.wrappedErrback angular.js:10952
deferred.promise.then.wrappedErrback angular.js:10952
(anonymous function) angular.js:11078
$get.Scope.$eval angular.js:11949
$get.Scope.$digest angular.js:11775
$delegate.__proto__.$digest VM437152:844
$get.Scope.$apply angular.js:12055
$delegate.__proto__.$apply VM437152:855
done angular.js:7837
completeRequest angular.js:8020
xhr.onreadystatechange angular.js:7976
2
  • 1
    This is unrelated to your $http.post. You're calling $digest or $apply somewhere in your code while a digest cycle is already in progress Commented May 14, 2014 at 19:37
  • 1
    Kinda hard to tell with this code, probably something somewhere else. but probably something to do with wrapping your posts in a loop, I'd wrap your $http.post calls in $q.all() then you can have a singular .then(). Commented May 14, 2014 at 19:37

2 Answers 2

1

Iam not sure if it's somewhere in your example or just something you do wrong elsewhere. Down below is a working example of a POST in angular.

var config = {
    method: 'POST',
    url: '/api/studentplannedcredit/',
    data: variablesToSend 
};


$http(config)
.success(function(data, status, headers, config) {
    console.log(data);
})
.error(function(data, status, headers, config) {
    console.log(data);
});

PS: Dont POST for each loop.. that will hammer the API badly.. collect the data first and then send 1 POST only.

Sign up to request clarification or add additional context in comments.

Comments

0

I am assuming, $modalInstance is the issue here.

Try to run without

$modalInstance.close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.