2014-07-18 217 views
0

我需要清除一個文本框和選擇列表後,按鈕clicked.This是我試過,但它似乎並不工作:如何清除輸入字段Angularjs一個按鈕,點擊後

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

http://stackoverflow.com/questions/15305764/angularjs-clear-input-text-with的-button –

+0

@Can Sahin Bakir我看到了那個鏈接,但它對我不起作用 – user3811714

回答

1

你缺少Model.在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 = ''; 
    } 
}); 
+0

或者簡單地說'$ scope.Model = new Model($ http);'取決於'Model'是什麼。您也可以使用['$ injector.instantiate'](https://docs.angularjs.org/api/auto/service/$injector#instantiate),而不是通過和創建模型,並讓Angular負責注入。 –

+0

沒錯,我只是假設OP不想銷燬現有的模型,如果你不介意 – maurycy

+0

@maurycy非常感謝,我會將你的建議添加到我的答案中,我沒有意識到我不得不把模型 – user3811714

3

代替使用按鈕清除輸入字段可以使用復位(INPUT TYPE =「復位」)的形式的內部

相關問題