0

I'm starting to learn angularJS in asp net so i can implement some data binding that is refresh every x time async and avoid using update panels. I managed to get the databinding to work, here is my code.

    <head runat="server">
    <title></title>

    <script src="Scripts/angular.min.js"></script>  

    <script>

        var app = angular.module("myModule", []).controller("myController",
            function($scope, $http) {

  $http.get("UsersService.asmx/GetAllUsers").then(function(response) {

                    $scope.users = response.data;

                });
            });

    </script>

</head>
<body ng-app="myModule">
    <form id="form1" runat="server">
    <div ng-controller="myController">

       <table>
           <thead>
               <tr>
                   <th>ID</th>
                   <th>Username</th>
                   <th>Password</th>
                   <th>LastLogin</th>
               </tr>
           </thead>
           <tbody>
               <tr ng-repeat="user in users">
                   <td>{{ user.ID }}</td>
                   <td>{{ user.Username }}</td>
                   <td>{{ user.Password }}</td>
                   <td>{{ user.LastLogin }}</td>
               </tr>
           </tbody>
       </table>

    </div>
    </form>
</body>
</html>

and the webservice

        [WebMethod]
        public void GetAllUsers()
        {
            List<User> listUsers = new List<User>();
            string cs = ConfigurationManager.ConnectionStrings["AngularJS_ConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Users", con))
            {
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        User user = new User();
                        user.ID = Convert.ToInt32(sdr["UserID"]);
                        user.Username = sdr["Username"].ToString();
                        user.Password = sdr["Password"].ToString();
                        user.LastLogin = sdr["LastLogin"] as DateTime?;
                        listUsers.Add(user);
                    }
                }
            }

            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Write(js.Serialize(listUsers));
        }

My question is, how can i for example, make it so that the webservice is called every x time and refreshed on the page using ajax and angular js?

2
  • Do you want to fetch few data from total data? For eg. 10 records each time till all the records are fetched. Let say you have total 100 records. Commented Feb 27, 2019 at 4:37
  • It's just like 3 to 2 rows, i'm making a notification system that shows just counts Commented Feb 27, 2019 at 8:54

1 Answer 1

1

Use setInterval in javascript

//first argument is function to call in interval
setInterval(function () {
    $http.get("UsersService.asmx/GetAllUsers").then(function (response) {

        $scope.users = response.data;
    }
}, 1000); //milliseconds
Sign up to request clarification or add additional context in comments.

Comments

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.