0

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

2
  • Could you please add the correct markup, bcz there is nothing caled 'id' in your markup Commented Feb 11, 2015 at 7:44
  • Shouldn't your ng-model have 'series' instead of 'serie'? Commented Feb 11, 2015 at 7:48

1 Answer 1

1

Change the 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:

$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);
        });
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.