4

Just trying to understand why in this case I would need to use $scope.$apply ? According to all the info I've read about the subject, I shouldn't have to, since everything is happening inside Angular? What am I missing?

In my controller:

$scope.savePreset = function(columns, $event){
  $event.preventDefault();
  preset.name = prompt("Please enter preset name", "My Preset");
  if (preset.name != null) {
    preset.columns = $scope.columns;
    $scope.presets[preset.name] = preset; // (Object)
    // Without $scope.$apply() here, view isn't updated. Why?
    alert('Your preset "' + preset.name + '" has been saved.');
    $scope.loadPreset(preset.name);
  } else {
    alert('Please enter a valid name.');
  }
}

(the savePreset function is called in an ng-click directive, and there's an ng-repeat for preset in presets, which isn't updating).

Thanks, Pim

Pim
  • 756
  • 1
  • 7
  • 18

2 Answers2

3

as mentioned in comment @Sergiu Paraschiv using functions which wrapped in window was stopping angular $digest cycle that's why my advise is to use $window provider and $window.prompt and $window.alert functions I think they are will work

here is a example

Narek Mamikonyan
  • 4,601
  • 2
  • 24
  • 30
1

~Prompt, alert.. will pause the execution of code (blocks the thread), to prevent the likes of setInterval, setTimeout to go crazy .

The $digest loop is made up of two smaller loops: when you do a prompt. $evalAsync is used, and the code inside is executed outside angular.This is usually done with setTimeout(0).

So In one sentence: this is prompt's fault, you might be better off with a widget that does the same thing.

Edit: Sorry submitted to fast: here is the original answer: Error: $digest already in progress in angularjs when using alert

Community
  • 1
  • 1
Abdoul Sy
  • 580
  • 3
  • 10
  • 1
    You just copied the answer here: http://stackoverflow.com/questions/16351184/error-digest-already-in-progress-in-angularjs-when-using-alert At least link to it... – Sergiu Paraschiv Oct 23 '14 at 12:20
  • This is based of the same answer, but didn't know how to formulate it, so I summarized the article; but is submitted before editing it, thanks for pointing that out though – Abdoul Sy Oct 23 '14 at 12:28
  • As long as you link to the original answer all is OK. – Sergiu Paraschiv Oct 23 '14 at 12:32
  • Now I'm not sure should I mark this as answered or not? :-) + @Sergiu Paraschiv was first, but in comment. Thank you both for the explanation, I suspected smthg like this. – Pim Oct 23 '14 at 12:33