Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS update value from AJAX callback

When I'm updating scope value directly, it updates in view just fine, but when this value is updated from AJAX callback it doesn't update. Here is simplified example - http://jsfiddle.net/hS8Bs/1/

How can I get around it?

Update: I noticed that clicking second time on the link does update the value, but it's not what I'm looking for.

like image 266
arnaslu Avatar asked Sep 20 '12 14:09

arnaslu


People also ask

What is the use of Ajax in AngularJS?

The AngularJS provides a control service named as AJAX – $http, which serves the task for reading all the data that is available on the remote servers. The demand for the requirement of desired records gets met when the server makes the database call by using the browser. The data is mostly needed in JSON format.

How do I connect to back end services in angular?

Communicating with backend services using HTTP link Most front-end applications need to communicate with a server over the HTTP protocol, in order to download or upload data and access other back-end services. Angular provides a client HTTP API for Angular applications, the HttpClient service class in @angular/common/ http.

How do I run JavaScript code when a value changes in AngularJS?

AngularJS binding, with ng-model and the double curly bracket syntax, does not allow you to run JavaScript code when a value changes. Thankfully, it does provide a $scope function, $watch (), for this purpose. Let's set up a simple watch to show how we can run additional code when values change. The $watch () is a function on the $scope service.

What is the use of $HTTP in Angular JS?

The $http is another Angular JS service which is used to read data from remote servers. The most popular form of data which is read from servers is data in the JSON format.


3 Answers

The real problem is the lack of $scope.$apply. When you are updating the angular model outside of the angular digest you should use apply.

$.getJSON('/echo/json/', {}, function(data){
    $scope.$apply(function(){
        $scope.period = '2010 - 2011';
    });
}); 

This will trigger the diggest to see the update and if your code inside of apply throw an exception it will be redirected to the $exceptionHandler service.

like image 127
Renan Tomal Fernandes Avatar answered Oct 20 '22 20:10

Renan Tomal Fernandes


This is an addon to the selected answer, I found that Angular JS still called "Error: $digest already in progress" when I implemented the $apply() function.

So I found Will Vincent's code on this post: AngularJS : What's the Angular way to interact with a form? , it checks for $digest already in progress and only calls $apply() when safe to:

$scope.safeApply = function(fn) {
    var phase = this.$root.$$phase;
    if(phase == '$apply' || phase == '$digest') {
        if(fn && (typeof(fn) === 'function')) {
          fn();
        }
    } else {
       this.$apply(fn);
    }
};

so it would be something like:

$scope.safeApply(function() { ...code here... });

like image 29
ako977 Avatar answered Oct 20 '22 22:10

ako977


Here is your missing tutorial you need to use $http parameter of controller in order to get field updated. Check your updated example http://jsfiddle.net/hS8Bs/2/

like image 35
vittore Avatar answered Oct 20 '22 22:10

vittore