尝试重置表单时出现错误,无法读取未定义的属性'$ 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是未定义的,因为您需要将其作为服务添加到顶部的函数中
function ($scope,$setPristine){...
这会让函数知道$ setPristine的意思。
另外,Angular JS $ setPristine仅在angularjs 1.1。*中有效。
关于javascript - AngularJS无法读取未定义的属性'$ setPristine',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32368630/