将数据插入输入值 - AngularJS

时间:2015-02-11 07:35:29

标签: javascript angularjs

我有两个这样的功能:

$scope.showSerieById = function (id) {

 $http.get('/api/series/' + id) 
  .success(function(data, status, headers, config) {
    $scope.series = data;
        console.log(data);
    })
    .error(function(data, status, headers, config) {
        console.log('Error: ' + data);
    });
};

$scope.editSeries = function (id) {
  var data = { title :  $scope.title , season: $scope.season, chapter : $scope.chapter, mark : $scope.mark, image: $scope.image};

  $http.put('/api/series/' + id, data)
    .success(function(data, status, headers, config) {
        $scope.series.push(data);
        console.log(data);
    })
    .error(function(data,status,headers,config) {
        console.log('Error: ' + data);
    });
};

我想知道如何将GET数据(按ID搜索)插入表单,然后使用PUT发送并更新数据。表格如下(不正确):

    <form data-ng-repeat="serie in series">
        <input type="text" ng-model="serie.title" value="{{series.title}}">
        <input type="text" ng-model="serie.season" placeholder="Season...">
        <input type="text" ng-model="serie.chapter" placeholder="Chapter...">
        <input type="text" ng-model="serie.mark" placeholder="Mark...">
        <input type="text" ng-model="serie.image" placeholder="Image...">
        </br></br>
        <button ng-click="addSeries()" class="btn">Add</button>
        <button ng-click="editSeries(serie._id)" class="btn">Update</button>
    </form>

非常感谢你!

1 个答案:

答案 0 :(得分:1)

更改editSeries

<form data-ng-repeat="serie in series">
    <input type="text" ng-model="serie.title" value="{{series.title}}">
    <input type="text" ng-model="serie.season" placeholder="Season...">
    <input type="text" ng-model="serie.chapter" placeholder="Chapter...">
    <input type="text" ng-model="serie.mark" placeholder="Mark...">
    <input type="text" ng-model="serie.image" placeholder="Image...">
    </br></br>
    <button ng-click="addSeries()" class="btn">Add</button>
    <button ng-click="editSeries(serie)" class="btn">Update</button>
</form>

在控制器中,“serie”对象作为参数传递:

$scope.editSeries = function (serie) {
    $http.put('/api/series/' + serie.id, serie)
        .success(function(data, status, headers, config) {
            //$scope.series.push(data); //No need for push
            console.log(data);
        })
        .error(function(data,status,headers,config) {
            console.log('Error: ' + data);
        });
};