简体   繁体   English

如何在angularjs中刷新和重新绑定db中的数据

[英]how to refresh and rebind the data from db in angularjs

hi all iam using angularjs ngrepeat to bind the datas into table.i have one add new button when i click bootstrap model popup open i fill the input details click submit means data will stored correctly but table couldn't not get the new data but once i reload the page data will show 嗨所有iam使用angularjs ngrepeat将数据绑定到table.i有一个添加新按钮,当我点击bootstrap模型弹出打开我填写输入详细信息单击提交意味着数据将正确存储但表不能得到新数据但一次我重新加载页面数据将显示

my controller code 我的控制器代码

var refresh = function () {
        $http.get('/ViewFacility').success(function (response) {
            $scope.ViewFacilitys = response;


    };

    refresh();

My add new code: 我添加新代码:

$scope.AddRole = function () {

        $http.post('/AddNewRole', $scope.Role).success(function (response) {

            refresh();
        });
    };

Html Code Html代码

  <form name="profileform">
            <div class="modal fade" id="myModal" role="dialog" ng-controller="IndexController">
                <div class="modal-dialog">

                    <!-- Modal content-->
                    <div class="modal-content" style="margin-top:135px">
                        <div class="modal-header">

                            <h4 class="modal-title ">Role Name</h4>
                        </div>
                        <div class="modal-body">

                            <h4>Name</h4>
                            <input type="text" name="RoleName" class="form-control" ng-model="Role.RoleName">
                            <span class="error" ng-show="profileform.FirstName.$invalid && profileform.FirstName.$dirty">Please enter a First Name</span>

                            <h4>Description</h4>
                            <input type="text" name="Description" class="form-control" ng-model="Role.Description">
                            <span class="error" ng-show="profileform.LastName.$invalid && profileform.LastName.$dirty">Please enter a Last Name</span>

                            <h4>IsActive</h4>
                            <input type="checkbox" name="IsActive" class="form-control checkbox" ng-model="Role.IsActive" style="margin-left:-47%" >
                            <span class="error" ng-show="profileform.Email.$invalid && profileform.Email.$dirty">Please enter a Email</span>
                        </div>
                        <div class="modal-footer">
                            <button class="btn btn-primary" ng-click="AddRole()" ng-disabled="profileform.$invalid">Submit</button>&nbsp;&nbsp;
                            <button class="btn btn-primary" data-dismiss="modal" ng-click="deselect()">Clear</button>&nbsp;&nbsp;&nbsp;&nbsp;
                        </div>
                    </div>
                </div>
            </div>
        </form>

在此输入图像描述

Just add the new item to the array. 只需将新项添加到数组即可。

$scope.AddRole = function () {
    $http.post('/AddNewRole', $scope.Role).success(function (response) {
       $scope.ViewFacilitys.push($scope.Role);
    });
};

You don't need to fetch all data each time you create a new item. 每次创建新项目时都不需要获取所有数据。 Refresh must be called just one time. 必须只调用一次刷新。

For pagination you can code a simple function that send the number of page to the server: 对于分页,您可以编写一个简单的函数,将页数发送到服务器:

$scope.changePage = function (page) {
    $scope.get('/ViewFacility?page='+page)
       .then(function (response) {
          $scope.ViewFacilitys = response.data;
       });
}

Try modifying your refresh function like so 尝试像这样修改refresh功能

var refresh = function () {
        $http.get('/ViewFacility').success(function (response) { //assuming this only fetches the newly added one
            $scope.ViewFacilitys.push(response);
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM