btmach
btmach

Reputation: 375

One value won't save in AngularJS

For some reason the comment.content part turns out to be null every time I try to save it, but the comment.pid works just fine, why is that? It's really not making sense to me.

The controller:

var CreateCommentController = function ($scope, $location, $routeParams, Comment) {
    $scope.action = "Create";
    var id = $routeParams.postId;
    var content = $scope.content;

    var comment = new Comment();
    comment.pid = id;
    comment.content = content;

    $scope.save = function () {
        Comment.save(comment, function () {
            $location = '/';
        });
    }
};

The HTML:

<div class="control-group" ng-class="{error: form.content.$invalid}">
    <label class="control-label lbl" for="content">Content:</label>
          <div class="controls">
               <textarea ng-model="content" id="content" class="textareaContent">content</textarea>
          </div>

Upvotes: 0

Views: 45

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039130

Try ng-model="comment.content" directive on the textarea. Also you should remove the optional content you provided between the opening and closing textarea tags. If you want to initialize it with some default content that should be done on the model:

<textarea ng-model="comment.content" id="content" class="textareaContent" ></textarea>

Also you might need to pass the comment instance in the scope:

$scope.comment = comment;

Upvotes: 1

Related Questions