- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试重置表单时,出现错误无法读取未定义的属性“$setPristine”。
我的 Controller :
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/
我正在尝试创建一个基本的登录表单,当加载页面并提交表单时,表单将重置。 我不断收到以下错误: TypeError: Cannot read property '$setPristine' of und
我有一个包含多个子表单的父表单(例如 http://jsfiddle.net/riemersebastian/9zr00ear/3/ ) 当用户更改任何字段时,相应的子表单和父表单都会获取类 ng-d
我正在尝试对表单中的单个输入字段使用 setPristine 方法。有多种方法可以对整个表单执行此操作,但我只想对表单中的一个输入字段执行此操作。我该怎么做? 我这样试过 $scope.merchan
**** 注意我删掉了一些其他功能,以便可以更快地阅读代码。我无法清除表格。当我单击具有取消功能的按钮时。我以为我可以设置一个默认表单,但这并没有什么不同。
First Name This is required.
提交表单后,我一直在努力重置表单。有人发布了此Here我想让它发挥作用但没有成功。这是我的My Code Example . $scope.form.$setPristine(); 未将 Pristi
我发现下面的代码很奇怪。 dataSvc.saves 回调之后,vm.bookDetails = {};和 vm.bookForm.$setPristine 的用途? angular.module('
尝试重置表单时,出现错误无法读取未定义的属性“$setPristine”。 我的 Controller : formApp.controller('FormController', ['$scope'
我正在尝试使用 $setPristine() 重置表单。 $scope.resetDataEntryForm = function() { $scope.dataEntryForm.$setP
我想做简单的事情,清理 $scope.user 字段,并且不会因此而出现错误。 if ($scope.contactForm.$valid) { $scope.user = {}; $
当我提交表单时,我希望某些输入元素为 $setPristine。因为在提交表单后,我将清空绑定(bind)到输入元素的模型,以防用户完全可以再次输入新内容。但是,一旦我清空模型,输入元素也会清空,因此
任何人都可以帮助使用随附的 Plunker 吗?? 只需提交仅输入一个必填字段的表单。这将触发验证错误。然后重置同时调用 setPristine 和 setUntouched 的表单 - 两者都应该重
我正在尝试使用 Angular 的内置表单函数,特别是 setPristine() 来清除用户提交的表单。我的 Controller 可以访问 $scope.newForm(我的表单)及其所有方法,但
当我运行以下代码来重置表单时出现错误: $scope.saveFormData = function () { $scope.testForm.$setPristine();
我有一个带有密码字段的表单,我为其实现了一个password 指令。 我目前只实现了 1 项验证,但我有一个现场验证列表。 我希望它们分别显示为红色或绿色,具体取决于有效/无效 - 当用户开始在字段中
我正在尝试将当前的 api 调用写入可在 ApiService 工厂内重用的工厂。但是,我似乎无法清除表格。 那么,我能做些什么来使clearForm函数按原样工作。 S
我有以下代码: 这是有效的,当我点击一个提交类型的按钮时,就会调用 modalSubmit 函数。 但是我想在我的 Controller 中做到这一点: $scope.mod
我有一个表单不允许您选中第一个条目上的复选框,这很奇怪。这几乎就像它没有记录点击一样。 但是,如果您提交表单并在提交第一个表单后尝试选中相同的复选框,则它允许您选中这些复选框。这是为什么? 这就是我的
我正在用 Angular Material 制作一个简单的表格来添加帖子,我还有一个取消按钮来清除表格。我尝试使用 $setPristine() 但在调用 clearForm 函数后输入字段仍显示为红
当我尝试使用 TypeScript 中的 form.$setPristine 时,它不起作用并且调试显示 form 未定义。根据我的阅读,$scope.formName.$setPristine()
我是一名优秀的程序员,十分优秀!