Kichu
Kichu

Reputation: 1731

Clear ng-model value in controller after ng-click

I need to change the ng-model value to empty after ng-click

My code:

<div class="desc-gestures-comment ind-row">
  <textarea id="txtCommentArea" class="comment-box text-comment-listing" name="comment" placeholder="Your comment" data-ng-model="newCommentTxt">  </textarea>

  <p class="text-center"><span class="btn-common btn-blue btn-large" data-ng-click="saveParentComment(newCommentTxt)">Post Comment</span></p>
</div>

Controller function

$scope.saveParentComment = function(newCommentTxt){
  alert(newCommentTxt)
  $scope.newCommentTxt = '';
}

After the $scope.saveParentComment, I need to change the newCommentTxt value to empty.

Is it possible ?

Please suggest solution.

Upvotes: 3

Views: 20729

Answers (3)

Nilesh Patil
Nilesh Patil

Reputation: 43

If you just want to set empty string to $scope.newCommentTxt, then there is no need to do it inside a function.

You can set empty string inside html code itself.

data-ng-click="saveParentComment();newCommentTxt=''";

Upvotes: 1

Rana Ahmer Yasin
Rana Ahmer Yasin

Reputation: 537

Just make a empty scope variable like $scope.newCommentTxt = ''; the variable will be empty when the ng-click functions is called the should be like this

Post Comment

Upvotes: -3

Sajeetharan
Sajeetharan

Reputation: 222582

You dont have to pass the value inside a function since the scope variable is already there, just need to make the scope variable empty in your function,

 $scope.saveParentComment = function () {
        $scope.newCommentTxt = "";
    };

Here is the sample Application

Upvotes: 4

Related Questions