0

enter image description herei am making simple http request to doGet method of servlet to fetch data from table. bellow is my code.

var myApp = angular.module('myApp', []);

myApp.controller('myController', function ($scope,$http) {


$scope.getDataFromServer = function() { 

            $http    
({         
    method:'GET',
    url:'jasoncontroller'    
}).success(function(data, status, headers, config){ 
                $scope.sqldata = data;                    

                console.log("executing");
                $scope.$watch('sqldata',function(newValue,oldValue){
                    console.info('changed');
                    console.log('new : ' + newValue);
                    console.log('old : ' + oldValue);
                    console.log('watch end');                    
                });            


}).error(function(data, status, headers, config){});

    };   

});

servlet doGet method

    protected void doGet(HttpServletRequest request, HttpServletResponse           response) throws ServletException, IOException {     
       System.out.println("Executing get");     
    getSqlData sql = new getSqlData();          
    Gson gson = new GsonBuilder().serializeNulls().create();        
    String tabname="saleq315";                      
    String json = gson.toJson(sql.gettabledata(tabname));       
    response.setContentType("application/json");
    response.getWriter().write(json);
    System.out.println("data sent");        
}

Whenever i click to fetch data it never get me new data once i change the table name in tabname="saleq315"; after multiple click table data refresh. and when i log this to console i see http request getting executed mulpliple times. If you see the image attached, i changed the table name and click to get the data,

first click - same table data --> console log one entry
seconds click - same table data --> console log one entry
third click - got new data --> console log 5 entries.
 Not sure what is happening here.

[![enter image description here][1]][1]
2
  • 1
    From the documentation : 'The watchExpression is called on every call to $digest() and should return the value that will be watched. (watchExpression should not change its value when executed multiple times with the same input because it may be executed multiple times by $digest(). That is, watchExpression should be idempotent'. That means you have to check equality of oldValue and newValue before doing something. By the way I hope you are not calling getDataFromServer multiple times because you will create multiple watchers. Commented Sep 8, 2015 at 8:50
  • 2
    You should create the $watch only once and outside the callback of http call. Commented Sep 8, 2015 at 8:55

0

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.