javascript - 如何在不刷新页面的情况下更新 Angular 数据?

标签 javascript jquery angularjs codeigniter

我在数据库表中有一些数据,我使用 Angular 来显示它。我使用页脚中的以下脚本来执行此操作:

 <script !src="">
var app = angular.module('enterprise', []);

app.controller('entercontroller', function($scope, $http){

    $scope.loadData= function(){

        $http.post('<?php echo base_url(); ?>Groups_/load_data')
            .then(function(mydata){
                console.log(mydata);

                $scope.datas = mydata.data;

            });
    };
    $scope.loadData();
});
</script>  

我的 Controller 中的 load_data() 函数如下:

 public function load_data(){
    $data["groups"] = $this->Groups->fetch_groups();
    return $this->output
    ->set_status_header(200)
    ->set_content_type('application/json', 'utf-8')
    ->set_output(json_encode($data["groups"], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
}

我的观点是一些带有提交按钮的输入。我需要的是,当我单击按钮添加新记录时,表会自动更新,而无需每次刷新页面。所以我制作了另一个脚本来执行此操作

 <script>
$(document).ready(function() {
$('#add').on('submit', function() {
    var that = $(this);
    $dataString = that.serialize();             
    $.ajax({
        url: '<?php echo base_url(); ?>'+ $("#uri").val() ,
        type: 'POST',
        dataType: 'json',
        cache:false,
        data : $dataString,
        success: function (data) {
            console.log(data);
            $('#message').html(data['message']);
            $(':input','#add')
              .removeAttr('checked')
              .removeAttr('selected')
              .not(':button, :submit, :reset, :hidden, :radio, :checkbox')
              .val('');
        },
        error: function (data) {
            console.log('error');
            console.log(data);
        }
    });
    return false;            
});
});
</script>

HTML 代码如下:

<div class="page-wrapper" ng-app="enterprise" ng-controller="entercontroller">
<div class="container-fluid">
<div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-block">
                    <form class="floating-labels m-t-40" method="post" id="add" action="addgroup">
                        <div class="form-group m-b-40">
                            <input type="text" name="group_title" class="form-control input-lg" id="input8" required><span class="bar"></span>
                            <label for="input8">Group Name</label><br />
                        </div>

                        <div class="form-group m-b-40">
                            <input type="text" name="group_link" class="form-control input-lg" id="input7" required><span class="bar"></span>
                            <label for="input7">Link</label>
                        </div>

                        <div class="form-group m-b-40">
                            <select name="group_icon_code" class="custom-select form-control" id="location1" required >
                                <option value="">Icon</option>
                                <?php
                                if(isset($icons))
                                    foreach($icons as $icon)
                                        echo '<option value="'.$icon->icon_value.'">'.$icon->icon_name.'</option>';
                                ?>
                            </select>
                        </div>

                        <div class="text-xs-right">
                            <input type="hidden" id="uri" name="uri" value="Groups_/addgroup" />
                            <button type="submit" ng-click="loadData()" name="add" value="1" class="btn btn-success"> <i class="fa fa-check"></i> save</button>
                            <button type="reset" class="btn btn-inverse"> <i class="fa fa-times"></i> cancel</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
<div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-block">
                    <div class="table-responsive">
                        <table class="table color-bordered-table inverse-bordered-table">
                            <thead>
                                <tr>
                                    <th>title</th>
                                    <th>link</th>
                                    <th>order</th>
                                    <th>icon</th>
                                    <th class="text-nowrap">control</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr ng-repeat="d in datas">
                                    <td>{{ d.group_title }}</td>
                                    <td>{{ d.group_link }}</td>
                                    <td>{{ d.group_order }}</td>
                                    <td><i class="mdi {{ d.group_icon_code }}"></i></td>
                                    <td class="text-nowrap">
                                        <a href="#" data-toggle="tooltip" data-original-title="Edit"> <i class="fa fa-pencil text-inverse m-r-10"></i> </a>
                                        <a href="#" data-toggle="tooltip" data-original-title="Delete"> <i class="fa fa-close text-danger"></i> </a>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
  </div>
</div>

我尝试将 Angular 脚本放入单击功能中,但它不起作用。 我需要的是添加后再次重新加载相同的位置。 任何想法。

最佳答案

AngularJS 主要用于双向数据绑定(bind),这似乎正是您正在寻找的。如果您使用 Angular,则无需使用 JQuery 来更新数据。 $scope 中声明的所有变量在更改时都会自动刷新

<小时/>

因此,您在加载 Angular Controller 时已经发布了数据。 我会将其包含在附加到范围的函数中,因此当单击按钮时,您将发布对象并将其插入到表中。

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

app.controller('entercontroller', function($scope, $http){
  $scope.tableData = [{ text: "First row" }, { text: "second row" }];

  $scope.loadData = function () {
    // here you do your http post
    var newRow = { text: "New Row" };
    $scope.tableData.push(newRow); // do this inside your http callback function
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js">
</script>
<body ng-app="enterprise" ng-controller="entercontroller">
  <table>
    <tr ng-repeat="row in tableData">
      <td>
        {{row.text}}
      </td>
     </tr>
  </table>
  <button ng-click="loadData()">
    Load data
  </button>
</body>

关于javascript - 如何在不刷新页面的情况下更新 Angular 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46717049/

相关文章:

angularjs - Angular UI-Grid 和 D3 图形连续

javascript - highcharts 树形图堆栈未捕获 RangeError : Maximum call stack size exceeded

javascript - 使用 AngularJs 制作月度出勤报告

javascript - Firebase 数据库函数无法修改 angularjs 中的 $scope 变量

javascript - 通过 jQuery 重置表单不清除 ckeditor cktext_area 字段

jquery - 带有 jquery 进度条的 css 树文件夹列表

javascript - 在满足条件之前无法使用计时器调用嵌套函数?

javascript - 将 JavaScript 值传递给 PHP 或其他 PHP 类 (codeigniter)

javascript - 以相同的方式对两个数组进行排序

javascript - 我可以配置 Jade 来生成可读的缩进的 HTML 代码而不是单行流吗?