-1

I am new to angular-js and building a simple to-do application. I am creating a task and displaying the created task in a html table using ng-repeat. But the problem is that after posting the data, $scope.tasks variable is updated on controller side, but not in view. The view updates after refreshing the web page only and the task is added to html table. How can I make the view update after creating the task. Thanks in advance. Here is my code:

In my controller:

var app = angular.module('MyApp', ['ngMaterial', 'ngMessages']);
app.controller('DemoCtrl', function($scope,$mdSidenav,$mdDialog,$interval,$http,$mdToast) {
     $scope.tasks = []; 
   _refreshTaskData();  //initial refresh

  $scope.submitForm = function() {

        var description = "";
        var taskId = "";

        $scope.formData = {
                taskId: $scope.taskId,
                description: $scope.description,
         };

        $http({
            method: "POST",
            url: 'savetask',
            data:  angular.toJson($scope.formData),
            headers : {
                'Content-Type': 'application/json',
            }
        }).then(_success, _error);
      };

    function  _refreshTaskData() {
        $http({
                method : 'GET',
                url : 'getTask',

            }).then(function(res) { // success
                $scope.tasks = res.data;
            }, function(res) { // error
                console.log("Error: " + res.status + " : " + res.data);
            });
    }

    function _success(res) {
         $mdDialog.hide();
         console.log('in success function');
        _refreshTaskData(); ;
    }

    function _error(res) {
           //error handling
     }
}); 

In my view:

<table>
   <tr ng-repeat=" t in tasks">
      <td>{{t.id}}</td>
      <td>{{t.description}}</td>
   </tr>
</table>
1
  • Correct your html template {{t.description}} and use track by $index ng-repeat=" t in tasks track by $index" Commented Dec 10, 2018 at 6:38

2 Answers 2

0

You must understand that these JS frameworks are asynchronous. Now what happens is, if you do an API call and make another API call whose result is based on the first one, the console does not wait for the result from one API and directly moves forward. SO what's happening in your case is sometimes/many times, before the POST call is served, the controller is not able to get fresh data with GET in time, thus not updating the view. What you can possibly do is enforce the GET only when POST is served

$http({
        method: "POST",
        url: 'savetask',
        data:  angular.toJson($scope.formData),
        headers : {
            'Content-Type': 'application/json',
        }
    }).then(function(res){
         $http({
            method : 'GET',
            url : 'getTask',

        }).then(function(res) { // success
            $scope.tasks = res.data;
        }, function(err) { // error
            console.log("Error: " + err.status + " : " + err.data);
        });
    });

It would be best if you are sending a success message from the backend and checking before GET call

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

Comments

-2

I think you are not calling _refreshEmployeeData at any point of time. If you add that instead of _refreshTaskData in your JS, then you will be able to see the result in view.

Also kindly use ng-init to call the _refreshEmployeeData in the controller. That would be the best way to initialize the fields.

8 Comments

Actually I posted the wrong code. Now corrected. Thank you.
Can you make sure you are getting the response properly. If possible can you post the response JSON.
And why is it -1, the one who gave -1 please let me know the reason!
Yes. I am getting the response properly. Array size also increased by 1 after posting the data.
Can you print {{tasks}} directly into view and see whether its coming
|

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.