javascript - 刷新 Angular 数据asp net webservice

标签 javascript c# asp.net angularjs

我开始在 asp net 中学习 angularJS,这样我就可以实现一些数据绑定(bind),每次异步刷新一次,并避免使用更新面板。我设法使数据绑定(bind)正常工作,这是我的代码。

    <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>

和网络服务

        [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));
        }

我的问题是,我怎样才能做到每次调用一次Web服务并使用ajax和Angular JS在页面上刷新?

最佳答案

在 JavaScript 中使用 setInterval

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

        $scope.users = response.data;
    }
}, 1000); //milliseconds

关于javascript - 刷新 Angular 数据asp net webservice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54895700/

相关文章:

javascript - 如何使用生成器使 Promise 同步执行

c# - 何时在 ASP.NET MVC 中实例化 EF4 上下文?

php - PHP 是否支持 MVP 模式?

javascript - 在添加新标记之前如何从谷歌地图中删除所有标记

javascript - 多个ul逆序

javascript - 尝试使用 WebGL (regl) 的正交投影在屏幕空间坐标中绘制形状

javascript - 如何在 React 中将变量的值从一个组件传递到另一个组件

c# - 修改获取集合;枚举操作可能无法执行。异常(exception)

c# - WCF DualHTTPBINDING 消息安全

asp.net - 哪个最快?数据检索