Data in angular is getting displayed and disappearing when used $scope

1.1k Views Asked by At

My ejs file is :
Express App

</head>
<body data-ng-app="indexHomepage" data-ng-controller="indexController">
<div style="margin:100px;">
<h1>Express!</h1>


    <table data-ng-repeat="user in users">
        <tr>
            <td>
                <h4 align="center"><br/><br/>{{user}}<br/></h4>
            </td>
        </tr>
        <tr>
        </tr>
    </table>


</div>

<script src="controller/indexHomepageController.js"></script>
</body>
</html>

and my angular controller code is :

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

  indexHomepage.controller('indexController', ['$scope', '$http',     function ($scope, $http) {


  $scope.getUsers = function () {
      $http.get('/getUsers').success(function (response) {

          if (response.responseErrorCode == 503) {
              console.log("No users in database");
          }
          else {

              $scope.users = response;
          }

      });
  }

  }]);

when I put console.log in else part of controller instead of setting $scope.users, even then it's showing up and immediately getting disappeared. But if I remove the function and just keep :

      $http.get('/getUsers').success(function (response) {

          if (response.responseErrorCode == 503) {
              console.log("No users in database");
          }
          else {

              $scope.users = response;
          }

      });

here , it works.

Is there anything that I am missing?

1

There are 1 best solutions below

1
On

Do you having plunker Demo ? may be you use console.log outside the else block , and don't forget that you use $http service that the $ajax call is a promise .

$http.get('/getUsers').then(function (response) {

      if (response.responseErrorCode == 503) {
          console.log("No users in database");
      }
      else {

          $scope.users = response;

console.log('reponse is ' + JSON.stringfy(response)); }

  }).catch(function(err){

console.log('err' + err) ; })