繁体   English   中英

Angular.js:如何将唯一的ID和JSON数据http http到REST API?

[英]Angular.js: How to http put a unique id and json data to REST API?

我有以下模板文件:

<h1>{{title}}</h1>
<ul>
    <li ng-repeat="friend in friends">
        <input type="text" ng-model="friend.name">
        <input type="text" ng-model="friend.gender">
    </li>
</ul>

<button ng-click="save()" class="btn btn-primary">Save</button>

而且我有相应的控制器文件:

angular
  .module('app')
  .controller('homeCtrl', ['$scope', 'friends', '$http', function($scope, friends, $http) {
        $scope.title = "Home";
        $scope.friends = friends;
        $scope.save = function(){
            $http.put('http://localhost:8000/ip', friends)
            (JSON.stringify($scope.friends));
        };
    }]);
  • 我的API接收带有唯一MongoDB _id的PUT请求以更新单个文档。
  • 上面的模板为我的集合(mongodb模型)中的每个文档创建2个输入字段。
  • 如何更改代码,使每行旁边都有一个“保存”按钮,如果单击该按钮,将以以下格式发送放置请求: http://localhost:8000/ip/_id, jsondata

像这样: <button ng-click="save(friend._id)" ng-model="friend._id" class="btn btn-primary">Save</button>

在控制器中,如下所示: $http.put('http://westeros:9000/ip/:_id', friends)

的HTML:

<h1>{{title}}</h1>
<ul>
    <li ng-repeat="friend in friends">
        <input type="text" ng-model="friend.name">
        <input type="text" ng-model="friend.gender">
        <button ng-click="save(friend)" class="btn btn-primary">Save</button>
    </li>
</ul>

JS:

angular
  .module('app')
  .controller('homeCtrl', ['$scope', 'friends', '$http', function($scope, friends, $http) {
        $scope.title = "Home";
        $scope.friends = friends;
        $scope.save = function(friend){
            $http.put('http://localhost:8000/ip/'+ friend.id, friend )

        };
    }]);

暂无
暂无

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

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