Clear ng-model value in controller after ng-click

18,886

Solution 1

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

Solution 2

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=''";
Share:
18,886
Kichu
Author by

Kichu

Updated on June 26, 2022

Comments

  • Kichu
    Kichu about 2 years

    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.