user3811714 user3811714 - 1 year ago 71
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:


<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">
<button ng-click="StopCurrentDowntime()">Stop Downtime Event</button>


angular.module('myApp', [])

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

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

Answer Source

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 = '';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download