简体   繁体   English

我有角形JS表单,它已将数据成功插入数据库,但未显示在表单下方的列表中

[英]i have angular JS form it is inserting data successfully into database but not showing in table listing below the form

I have an HTML form that is inserting successfully data into a database, but is not showing the data in the table below it. 我有一个HTML表单,该表单已将数据成功插入数据库,但未在其下面的表中显示数据。 The only way to check we have to use is to manually refresh the page. 检查我们必须使用的唯一方法是手动刷新页面。 The same happen with delete, sometimes a dialog is shown, telling us that value was deleted, but we still have two referesh the page to see the change. 删除操作也会发生同样的情况,有时会显示一个对话框,告诉我们该值已删除,但是我们仍然有两个参照页面可以看到更改。 Is there any fucntion to reload that listing? 是否有重新加载该列表的功能? Any help would be appreciated 任何帮助,将不胜感激

Here is my angular html Named as : manage_features.html 这是我的有角html,名为:manage_features.html

 <!-- START panel--> <div class="panel panel-default"> <div class="panel-heading"> <h3>Manage Features Name</h3> </div> <div class="panel-body"> <form class="form-horizontal"> <div class="form-group"> <label class="col-lg-2 control-label">Name</label> <div class="col-lg-10"> <input type="text" ng-model="rec.Name" placeholder="Name" class="form-control"> </div> </div> <div class="form-group"> <div class="col-lg-offset-2 col-lg-10"> <div class="checkbox c-checkbox"> <label> <input type="checkbox" ng-model="rec.isSpecial"> <span class="fa fa-check"></span>is Special</label> </div> </div> </div> <div class="form-group"> <div class="col-lg-offset-2 col-lg-10"> <div class="checkbox c-checkbox"> <label> <input type="checkbox" ng-model="rec.isMultiple"> <span class="fa fa-check"></span>Is Multiple</label> </div> </div> </div> <div class="form-group"> <div class="col-lg-offset-2 col-lg-10"> <button type="button" ng-click="add(rec)" class="btn btn-sm btn-default">ADD</button> </div> </div> </form> </div> </div> <!-- END panel--> 

here is my JS file named as manage_features.js 这是我的JS文件,名为manage_features.js

 $scope.add = function(rec){ console.log(rec); $scope.upload = $upload.upload({ url: 'api/AdminArea/feature/add', //upload.php script, node.js route, or servlet url method: 'POST', headers: {'header-key': '83c238df1650bccb2d1aa4495723c63f07672ee8'}, withCredentials: true, data:{data:rec}, }).success(function(data, status, headers, config) { console.log(data); return false; toaster.pop('success', 'Add', 'Record added successfully.'); $scope.$root.$broadcast("myDocEventReload", {}); }); }; 

here is delete function 这是删除功能

 $scope.delete = function (id) { console.log(id);//return false; ngDialog.openConfirm({ template: 'modalDeleteDialogId', className: 'ngdialog-theme-default' }).then(function (value) { $http .post('api/AdminArea/feature/delete', {id:id}) .then(function (res) { console.log(res.data); if(res.data != 1){ toaster.pop('success', 'Deleted', 'Record deleted successfully.'); } else{ toaster.pop('error', 'Deleted', 'Record can\\'t be deleted!'); } }); }, function (reason) { console.log('Modal promise rejected. Reason: ', reason); }); //if(popupService.showPopup('Would you like to delete?')) { // } }; 

The main point, that I can basically see in your code, is that the object 'rec' is used 'freely' and for this reason when you receive the response from the server you are not populating the correct object. 我基本上可以在您的代码中看到的要点是,对象“ rec”是“自由使用”的,因此,当您收到来自服务器的响应时,您并不会填充正确的对象。

The reason for this is that when you are calling .then(function (res) { the 'res' variable is confined in the scope of that particular function, and won't change the value of the scope variable. 这样做的原因是,当您调用.then(function (res) { ,“ res”变量限制在该特定函数的范围内,并且不会更改scope变量的值。

my advice is to do something like this: 我的建议是做这样的事情:

create a controller 创建一个控制器

angular.module('mymodule',[]).controller('myCtrl',['$upload','toaster' function($upload, toaster){
    var self = this;
    self.rec = {};

    self.add = function(){
        $upload.upload({
        url: 'api/AdminArea/feature/add', //upload.php script, node.js route, or servlet url 
        method: 'POST', 
        headers: {'header-key': '83c238df1650bccb2d1aa4495723c63f07672ee8'}, 
        withCredentials: true, 
        data:{data:self.rec},
      }).success(function(data, status, headers, config) {
        /* THIS RETURN WAS BLOCKING THE NEXT, YOU HAVE TO REMOVE IT
        console.log(data);
        return false;*/

                 toaster.pop('success', 'Add', 'Record added successfully.');
                 /*$scope.$root.$broadcast("myDocEventReload", {});*/

      });
    }

    self.delete = function(id){
        console.log(id);//return false;
        ngDialog.openConfirm({
            template: 'modalDeleteDialogId',
            className: 'ngdialog-theme-default'
        }).then(function (value) {
            $http
                .post('api/AdminArea/feature/delete', {id:id})
                .then(function (response) {
                    console.log(response.data);
                    if(response.data != 1){
                        toaster.pop('success', 'Deleted', 'Record deleted successfully.');


                    }
                    else{
                        toaster.pop('error', 'Deleted', 'Record can\'t be deleted!');

                    }
                });
        }, function (reason) {
            console.log('Modal promise rejected. Reason: ', reason);
        });
     };
}

Then you can use this controller in your code with 然后,您可以在代码中使用此控制器

<div ng-controller='myCtrl as c'>
    <input type="checkbox" ng-model="c.rec.isMultiple">
</div>

When you change the object rec inside the controller the changes will be visible automatically in the html. 当您更改控制器内部的对象rec时,更改将自动在html中可见。

It's a simple answer to tell you the idea of what I think you should do with angular. 这是一个简单的答案,告诉您我认为应该使用角度的想法。

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

相关问题 显示带有角度js数据绑定形式的引导箱 - Showing the bootbox with the angular js data binded form Angular JS-通知用户是否使用HTML元素成功提交了表单 - Angular JS - Notify the user if form is successfully submitted using HTML element 使用JS在websql中插入表单数据创建动态表单 - Create dynamic form with inserting form data in websql using JS 使用JavaScript将表格插入表格 - Inserting Form into table with javascript UseMap将数据插入表单 - UseMap Inserting Data Into Form 使用js更新表单字段,但未在提交的数据中显示 - update form field with js but not showing in submitted data 如果表单数据提交成功,如何显示通知? - How can I show a notification if the form data was submitted successfully? 如何使用javascript和node.js-mysql而不使用php将HTML表单数据提交到MySql数据库表中? - How can I submit HTML form data into MySql database table using javascript along with node.js - mysql and without php? 我在同一页面上有一个表格和表格,如何使用表格来更新表格。 Express / Node.js - I have a form and table on same page, how do i use the form to update the table. Express / Node.js 用JS提交表单并成功发布所有表单数据的正确方法是什么? - What is the proper way to submit a form with JS and still post all form data successfully?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM