【问题标题】:Mean stack client side update平均堆栈客户端更新
【发布时间】:2014-09-28 21:11:30
【问题描述】:

我在实现客户端更新 CRUD 逻辑时遇到问题。当前正在按照原样删除这些字段。我错过了什么?

我的角度:

$scope.editService = function(id) {
        $http.put('/api/hc/' + id, 
                  {title: 'new',
                   shortname: 'new',
                  summary: 'new',
                  description: 'new'}
                  )
           .success(function(data) {

        })
        .error(function(data) {
            console.log('Error: ' + data);
        });
}; 

我的快递:似乎没有任何东西通过 JSON 值,由于某种原因,所有字段和键都被清除干净,只留下 _id 和 _v 键和值。

.put(function(req, res) {
    Service.findById(req.params._id, function(err, service) {
        if (err)
            res.send(err);
        service.title = req.body.title;     // update the items info
        service.summary = req.body.summary;
        service.shortname = req.body.shortname;
        service.description = req.body.description;
        // save the items
        service.save(function(err) {
            if (err)
                res.send(err);
            res.json({ message: 'Service updated!' });
        });
    });
})

我的看法

 <form name="editForm" ng-submit="editService(service._id)" ng-repeat="service in services 
 filter:json">
  <input type="text" placeholder="{{ service.title}}" ng-model="serviceTitle" required>
  <input type="text" placeholder="{{ service.shortname}}" ng-model="serviceShortname" required>
  <input type="text" placeholder="{{ service.description}}" ng-model="serviceSummary" required>
  <textarea type="text" placeholder="{{ service.summary}}" ng-model="serviceDescription" required></textarea>
  <button type="submit">Edit</button>
  </form>

【问题讨论】:

    标签: node.js angularjs express mean-stack


    【解决方案1】:

    您实际上并没有将数据放入您给出的示例中。

    $http.put('/api/hc/' + id)
    

    应该是

    $http.put('/api/hc/' + id, formData)
    

    formData 是您从要发送到管道的表单字段中收集的任何对象。另外,看看 Angular 的 $[resource][1] 服务,与直接使用 $http 相比,这是一种更简洁 (imo) 的 REST 客户端方式。

    【讨论】:

    • 会不会像添加一样简单: $scope.editService = function(id) { $http.put('/api/hc/' + id, formData) .success(function(data) { }) .error(function(data) { console.log('Error: ' + data); }); };
    • 是的,不过我也建议对成功进行某种 UI 更新。好吧,假设您已经构建了 formData;这不会自动进入。它通常是您将表单字段绑定到的 $scope 上的一个对象。
    • 嗯,由于某种原因,它具有相同的效果,并删除了 JSON 对象中的键和值。
    • 您的帮助让我更接近答案。我目前将字段替换为“新”。我如何在表单字段输入中获取这些信息?
    • 我在这篇文章stackoverflow.com/questions/25161885/… 中找到了答案,不过您的回答对我帮助很大。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    相关资源
    最近更新 更多