简体   繁体   English

将数据插入输入值-AngularJS

[英]Insert data into input value - AngularJS

I have a two functions like this: 我有两个这样的功能:

$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);
    });
};

I'd like to know how to insert GET data (search by ID) into a form and then send it using PUT and update data. 我想知道如何将GET数据(按ID搜索)插入表单,然后使用PUT发送并更新数据。 Form is the following (it's incorrect): 形式如下(不正确):

    <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>

Thank you so much! 非常感谢!

Change the editSeries 更改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>

In the controller, the "serie" object is passed as parameter: 在控制器中,“ 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);
        });
};

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

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