【问题标题】:refresh angular data asp net webservice刷新角度数据asp net webservice
【发布时间】:2019-07-20 14:21:18
【问题描述】:

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

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

我的问题是,例如,我怎样才能使它每 x 次调用一次 web 服务并使用 ajax 和 angular js 在页面上刷新?

【问题讨论】:

  • 您想从总数据中提取少量数据吗?例如。每次10条记录,直到所有记录都被提取。假设您总共有 100 条记录。
  • 这就像 3 到 2 行,我正在制作一个只显示计数的通知系统

标签: javascript c# asp.net angularjs


【解决方案1】:

在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

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 2017-06-30
    • 1970-01-01
    • 2021-10-30
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    相关资源
    最近更新 更多