user3811714 user3811714 - 4 months ago 14
AngularJS Question

How to clear input fields in Angularjs after click of a button

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 = '';
}
});

Answer

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 = '';
  }
});
Comments