注意:我在此處查看了解決方案的SO,但沒有人在對象中存在額外的函數問題。AngularJS:使用控制器作爲目標表單作爲對象的語法
我在我的角度JS應用的一種形式:
<div ng-app="plunker">
<div ng-controller="PMTController as pmt">
<form name="myForm">
<div class="form-group">
<input type="text" class="form-control" />
</div>
<button class="btn btn-primary" ng-click="pmt.search.resetSearchForm()">Reset</button>
</form>
</div>
</div>
另外,我有一個對象控制器:
app.controller('PMTController', function($log) {
var _this = this;
_this.search = {
resetSearchForm: function() {
$log.debug('test');
// how to target the form?
}
};
})
我NG單擊作品,作爲log.debug作品。但沒有任何調整的目標表單,以便我可以重置整個事情(清空所有的領域)的作品。
我可以做$window.myForm.reset();
但我怎麼能從角度做到這一點?
請注意我的主要問題/問題是如何正確從在search
對象resetSearchForm
函數內部目標的形式。
注意我試圖更改表格名稱爲pmt.myForm
或pmt.search.myForm
無濟於事。
我試過$setPristine
和$setUntouched()
但他們似乎沒有清除字段。
我知道我可以分配一個模型並將其分配給所有的窗體控件,但是這是一個原型,所以我寧願做一個簡單的重置。
我做了筆:https://codepen.io/smlombardi/pen/YWOPPq?editors=1011#0
您輸入ISN不以任何方式限制角度(沒有'ng-model')並且沒有'id',所以它不可能爲angul ar維護它的數據。 – Claies
我會嘗試它,但注意我似乎無法從該函數內正確定位窗體本身。我編輯了這個問題來澄清。 – Steve
,因爲創建用於表示表單的對象是一個角度特殊的對象,只包含它綁定的輸入。角沒有辦法改變它沒有綁定的表單字段的值。 – Claies