我收到錯誤嘗試重置表單時無法讀取未定義的屬性'$ setPristine'。
我的控制器:
formApp.controller('FormController', ['$scope',
function ($scope) {
$scope.options = ["Opt1", "Opt2", "Opt3", "Other"];
$scope.formData = {
selectedOption: null,
firstName: "",
lastName: "",
email: "",
phone: "",
fax: "",
comments: ""
};
var origData = angular.copy($scope.formData);
$scope.submit = function() {
// submit code goes here
};
$scope.reset = function() {
$scope.formData = angular.copy(origData);
// $scope.financeForm.$setUntouched();
$scope.financeForm.$setPristine();
};
$scope.reset();
}
]);
我的HTML(我已經去掉了多數領域保持這個最小):
<form id="financeForm" name="financeForm" ng-submit="financeForm.$valid && submit()" novalidate>
<md-content layout-padding class="autoScroll">
<md-input-container flex md-is-error="financeForm.selectedOption.$invalid && (financeForm.$submitted || financeForm.selectedOption.$dirty)">
<md-select required placeholder="Nature of your Enquiry" ng-model="formData.selectedOption" name="selectedOption" id="selectedOption">
<md-option ng-repeat="opt in options" value="{{opt}}">{{opt}}</md-option>
</md-select>
<div ng-messages="financeForm.selectedOption.$error" ng-if="financeForm.$submitted || financeForm.selectedOption.$touched">
<div ng-message="required">Please your enquiry option.</div>
</div>
</md-input-container>
<md-input-container>
<label>Comments</label>
<textarea ng-model="formData.comments" columns="1" md-maxlength="500"></textarea>
</md-input-container>
</md-content>
<md-button class="md-raised" ng-click="reset();">RESET</md-button>
<md-button class="md-raised md-primary">SUBMIT</md-button>
</form>
我不明白我在做什麼錯。有人可以幫忙嗎?
不應該$ setPristine在頂部添加爲服務,你的函數。就像範圍 –
,當您調用'$ scope.reset()'時,'financeForm'沒有被綁定到您的控制器範圍,它只在控制器被初始化爲AFAIK後才被綁定。當你的控制器初始化時,是否需要調用'$ scope.reset()'?當點擊你的重置按鈕時,你不應該得到那個錯誤,或者你呢? – user2718281