Skip to content Skip to sidebar Skip to footer

Changing Angularjs Scope Variables From An Inside Function

Hie I am new to Angular and I'm trying to change (or set) the scope variables from a function that is inside a controller. Here is my code: $.ajax({ type: 'POST', u

Solution 1:

Change your success callback:

functionsuccessFunc(data, status) {
  $scope.modalStatus = 'success';
  $scope.modalIcon = 'check';
  $scope.modalMessage = 'Invoice Saved!';
  showNotification();
  $scope.$apply();
}

Also it's a bad practise to use jQuery and Angular in one application. Angular provides $http fo requests.

Solution 2:

Why use Jquery $ajax while AngularJs provides $http.

Actually, there is an internal mechanism in AngularJs to apply changes, call it $digest. When you use JQuery, it is beyond angularJs scope and it does not apply $digest mechanism.

so in this case manually apply $digest by using following apply method inside successFunc function.

$scope.$apply(function() {
  $scope.modalStatus = 'success';
  $scope.modalIcon = 'check';
  $scope.modalMessage = 'Invoice Saved!';
});

this link may help you understanding more about $apply and $digest http://www.panda-os.com/2015/01/angularjs-apply-digest-and-evalasync/#.Vq1Z2lN95R0

Post a Comment for "Changing Angularjs Scope Variables From An Inside Function"