1

I just wanna post some changes to the database. This is my angular - the problem is, it dosn't take values from my html inputs.

See section 2 of code:

mainController = function ($scope, $http) {
    $scope.post = {};
    console.log($scope);
    console.log($scope.post);
    $http.get('/api/todos')
        .success(function(data) {
            $scope.posts = data.posts;
            $scope.datas = data;
            // console.log(data);
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });

    $scope.editTodo = function() {
        console.log($scope.post);
        $http.post('/post/edit', $scope.post)
            .success(function(data) {
                $scope.post = {};
                console.log($scope.post);
                $scope.posts = data.posts;
                $scope.datas = data;
            })
            .error(function(data) {
                console.log('Error: ' + data);
            });
    };
}

Section 2:

 <div class="comment" ng-repeat="post in posts | filter:search:strict | orderBy:predicate:reverse">
        <div class="comment-content">
          <form>
            <input type="text" class="" ng-model="post.id" />
            <input type="text" class="h2" ng-model="post.title" />
            <textarea ng-model="post.content" ></textarea>
            <span class="submitcontent" ng-click="editTodo()">
              Submit
            </span>
          </form>
        </div>
      </div>

I don't get why my $scope.post obj is allways equal to: Object {} and my log from the node server shows:

{ id: undefined, postTitle: undefined, postContent: undefined }
POST /post/edit 200 8ms - 2.92kb

1 Answer 1

3

In your ng-repeat directive post is the current post NOT $scope.post. Just pass the post you're dealing with to your edit function:

<span class="submitcontent" ng-click="editTodo(post)">Submit</span>

JavaScript:

$scope.editTodo = function(p) {
    console.log(p); // will have your id, title and content.
};
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.