【问题标题】:How to submit after using ng-repeat, ng-form and ng-submit使用 ng-repeat、ng-form 和 ng-submit 后如何提交
【发布时间】:2015-06-09 11:40:31
【问题描述】:

我正在使用一些数据来获取以下书籍列表:-

1 1001 - 500 - 指环王参考(InputBox) 备注(InputBox) SubmitBox

2 1002 - 570 - 哈利波特参考(InputBox) 备注(InputBox) 提交框

3 1003 - 720 - 达芬奇代码参考(InputBox) 备注(InputBox) SubmitBox

我可以通过下面的代码获得上述信息。但是,我不知道如何将数据保存到另一个数据库,如果有人可以指导我,将不胜感激。

这是我的html代码:-

    <form name="Bookentry" ng-submit="save()">

        <div  ng-repeat="lists in list.Books | orderBy: ['numB','numB1' ]">

            <ng-form name="item">

                {{$index+1}}

                <input type="checkbox" ng-click="delete(lists._id)">{{lists.numB}} - {{lists.numB1}} - {{lists.bookname}}
                <input type="text" name="Reference" ng-model="lists.Reference" ng-maxlength="8" ng-minlength="1"></input>
                <input type="text" name="Remarks" ng-model="lists.Remarks" ng-maxlength="8" ng-minlength="1"></input>
                <button type="submit">Save</button>
            </ng-form>
        </div>          
    </form>

我的 core.js 代码是:-

$scope.save = function(){
    $http.post('/api/save',$scope.Bookentry)
        .success(function(data){
            $scope.list = {};
            $scope.list.Books = data;
        })
        .error(function(data){
            console.log('Error: ' + data);
        });
};

我的 app.js 是:

app.post('/api/save', function(req,res){

  dbBooks.create({
    numB: req.body.numB,
    numB1: req.body.numB1,
    name: req.body.bookname,
    Reference: req.body.Reference,
    remarks: req.body.remarks,
    done: false
  }, function(err,result){
    if (err)
        res.send(err);
    dbBooks.find(function(err,result){
        if (err)
            res.send(err)
        res.json(result)
    });
  });
});

【问题讨论】:

  • 向您的服务器发送一个 JSON 数组并遍历该数组?
  • 我是初学者,需要更多解释。谢谢
  • 你使用什么样的 REST api?
  • 我不知道我用的是什么 REST api 之王。我反复试验并尝试了上述代码,但它不起作用
  • 如果我查看您的代码,我发现您正在向/api/save 发出 POST 请求。我的问题是,你用什么来处理对这个 URL 的这些请求?这可能是一个带有 PHP 或 JAVA(jax-rs) 的网络服务器。它应该是这样的 URL:http://example.com/api/save

标签: javascript angularjs node.js forms


【解决方案1】:

您可以将列表模型作为参数传递给保存函数。 html代码:

<form name="Bookentry" ng-submit="save(lists)">

核心js:

$scope.save = function(bookEntry){
    $http.post('/api/save',bookEntry)
        .success(function(data){
            $scope.list = {};
            $scope.list.Books = data;
        })
        .error(function(data){
            console.log('Error: ' + data);
        });
};

【讨论】:

  • 我试过了,但我如何在我的 app.js 中获取数据?谢谢
猜你喜欢
  • 1970-01-01
  • 2020-02-04
  • 2015-11-10
  • 2017-02-28
  • 2016-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-27
相关资源
最近更新 更多