Search code examples
angularjsangularjs-scopeangularjs-ng-click

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.


Solution

  • 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