0

I need to clear a textbox and a select-list after a button is clicked.This is what I tried but it doesn't seems to work:

HTML:

    <input type="text" ng-model="Model.CurrentDowntimeEvent.Comment" size="60" placeholder="ENTER IN ANY ADDITIONAL INFORMATION"/><br>
     <select class="categories" ng-disabled="selectlistdisabled"  ng-model="Model.CurrentDowntimeEvent.CategoryId" ng-options="downtimeCategory.CategoryId as downtimeCategory.CategoryName for downtimeCategory in Model.DowntimeCategories">     
   </select>
 <button ng-click="StopCurrentDowntime()">Stop Downtime Event</button>  

JS:

    angular.module('myApp', [])

     .controller('DowntimeController', function ($scope, $http) {
         $scope.Model = new Model($http);

     $scope.StopCurrentDowntime = function () {
             $scope.CurrentDowntimeEvent.Comment = '';
             $scope.CurrentDowntimeEvent.CategoryId = '';
}
});
2

2 Answers 2

3

Instead of using button to clear input fields you can use reset (input type="reset") inside the form

Sign up to request clarification or add additional context in comments.

Comments

1

You are missing Model. in your JS

angular.module('myApp', []).controller('DowntimeController', function ($scope, $http) {
   $scope.Model = new Model($http);

   $scope.StopCurrentDowntime = function () {
     $scope.Model.CurrentDowntimeEvent.Comment = '';
     $scope.Model.CurrentDowntimeEvent.CategoryId = '';
  }
});

5 Comments

Or simply $scope.Model = new Model($http); depending on what Model is. You could also use $injector.instantiate instead of creating the Model by and, and let Angular take care of injection.
True, I just assumed that OP don't want to destroy existing model, I'll add your suggestion to my answer if you don't mind
@maurycy thanks a lot hey,I didn't realize that I had to put Model as well
@CaioToOn I didnot understand this, can you please explain Why we use new Model?
If you don't need to reuse the same instance, you can simply create a new one and expose in the scope. By doing that, and considering it will bring an empty object, it would clear the fields in the same way. Does it make sense?

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.