0

Hi I'm learning AngularJS and I have a question. I want to display a table on a button click. On clicking the button, JSON data gets fetched but I have to press the button twice for the data to be displayed.

This is the HTML page.

<html>
  <body>
    <div class="container">
      <label for="tags" style="margin-top: 30px;margin-left: 15px;">GSTIN </label>
      <input id="tags">
        <button ng-click="searchfunction()">search</button>
    </div>
    <br/>
    <hr>
    <div class="container">
      <div ng-show="tshow"  ng-repeat="x in searchdata">
        <table class="table table-bordered table-responsive">
          <thead>
            <tr>
              <th>MON</th>
              <th>SGST</th>
              <th>CGST</th>
              <th>IGST</th>
              <th>CESS</th>
            </tr>
          </thead>
          <tbody>
            <tr ng-repeat="i in x">
              <td>{{i.mon}}</td>
              <td>{{i.sgst}}</td>
              <td>{{i.cgst}}</td>
              <td>{{i.igst}}</td>
              <td>{{i.cess}}</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </body>
</html>

This is the controller:

app.controller("searchcontroller", function ($scope,$http) {
  $scope.tshow=false;
  function make_base_auth(user, password) {
    var tok = user + ':' + password;
    var hash = btoa(tok);
    return "Basic " + hash;
  }

  $scope.searchfunction=function() {
    $scope.tshow=true;
    var tf=document.getElementById("tags");
    var value=tf.value;
    var auth = make_base_auth("gstadmn112","Gstn@123");
    var url6 = "http://164.100.148.67:8080/wsgstr3B/rest/payment/gstinsearch?gstin="+value+"&year=201718";
    xml = new XMLHttpRequest();
    // jQuery
    $.ajax({
      url : url6,
      method : 'GET',
      beforeSend : function(req) {
        req.setRequestHeader('Authorization', auth);
      },
      success:function(response) {
        console.log(response);
        scope.searchdata=response;
      },
      failure:function() {
        window.alert("wrong input data doesn't exist");
      }
    });
  }
});

I need to click twice on the search button for the table to be displayed. I want the table to be hidden initially and once the search is successful the table should be displayed. The table is hidden initially and after clicking twice correct data gets displayed.

dda
  • 6,030
  • 2
  • 25
  • 34
Amey Bhivshet
  • 79
  • 1
  • 10

3 Answers3

0

This problem is related to the digest loop of angularjs which keeps all changes sync between your view and controller.

When you invoke the searchfunction(), angularjs will know whats happening inside the method and sync the changes made with the view when its completed. The problem is that your method uses $.ajax which has some async callback methods. When these methods gets invoked angularjs have already left the party (digest loops is over) and don't know what these methods have done to your controller $scope. The jQuery success callback will however set the $scope.searchdata=response; and this change gets notified the next time angularjs is in the party (the next time you click).

So basically you need to make sure angularjs is aware of the async methods which makes changes to your $scope. To solve this I would inject angularjs own $http service (which takes care of async changes to the scope) and use that instead.

var req = {
 method: 'GET',
 url: url6,
 headers: {
   'Authorization': auth
 }
}
$http(req).then(function(response){
    console.log(response);
    $scope.searchdata=response;
}, function(){
    window.alert("wrong input data doesn't exist");
});
Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69
0

Maybe, you try to add $scope.tshow=true; in function success:

success:function(response) {
        console.log(response);
        $scope.tshow=true;
        $scope.searchdata=response;
      },

P.S. I advise to use $http instead of $ajax.

Aleksei Korkoza
  • 427
  • 7
  • 22
0

You can use this way.

$scope.searchfunction=function(){
$scope.tshow=true;
var tf=document.getElementById("tags");
var value=tf.value;

$http.get("http://164.100.148.67:8080/wsgstr3B/rest/payment/gstinsearch?gstin="+value+"&year=201718")
.success(function(result) {
     $scope.searchdata=response;
     $scope.tshow=false;
})
.error(function() {
     window.alert("wrong input data doesn't exist");
 });                 
}
Ayuktia
  • 431
  • 3
  • 11