Why is the view not updated in this particular case? ($ Scope. $ Apply)

Just trying to figure out why in this case I would need to use $ scope. $ apply? According to all the information I read about the subject, I don’t need to, since everything happens 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 function savePresetis called in the ng-click directive and there is ng-repeat for preset in presets, which is not updated).

Thanks Pim

+4
source share
2 answers

@Sergiu Paraschiv, , window, angular $digest, $window provider $window.prompt $window.alert , ,

+3

~ , .. ( ), setInterval, setTimeout .

$digest :   . $evalAsync , angular. setTimeout (0).

, : , , .

: , : : : $digest angularjs

+1

All Articles