- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
**** 注意我删掉了一些其他功能,以便可以更快地阅读代码。我无法清除表格。当我单击具有取消功能的按钮时。我以为我可以设置一个默认表单,但这并没有什么不同。
<form name="myForm" novalidate ng-submit="submit()">
<table class="mealCost">
<!-- descriptions -->
<tr>
<td> Base Meal Price: </td>
<td><input type="number" name="price" ng-model="mealPrice" required></td>
</tr>
<!-- waiter puts in the info -->
<tr>
<td> Tax Rate: % </td>
<td><input type="number" step="0.01" name="tax" ng-model="mealTax" required></td>
</tr>
<tr>
<td> Tip Percentage: % </td>
<td><input type="number" name="tip" step="0.01" ng-model="tipPercent" required></td>
</tr>
</table>
<p class="userResponse">
<input type="submit" value="Submit">
<!-- <input id="cancel" type="submit" value="Cancel" ng-submit="cancel(original)"> -->
<button ng-click="cancel()">Start Over</button>
</p>
</form>
var app = angular.module('myApp',[]).
controller('costController', function($scope) {
// $scope.ready= false;
$scope.mealPrice ="" ;
$scope.mealTax = 0.05;
$scope.tipPercent =0.05;
// possibly could do
var defaultForm={
price: "",
tax: "",
tip:""
}
$scope.cancel = function() {
$scope.myForm.$setPristine();
$scope.user = angular.copy(defaultForm);
console.log('empty');
}
最佳答案
我认为你用错了。
$setPristine:
“可以调用此方法来删除 'ng-dirty' 类并将表单设置为其原始状态(ng-pristine 类)。此方法还将传播到此表单中包含的所有控件。”
所以这只会清除类而不是 $scope 变量。
您确实重置了 $scope.user 变量,可以说:
添加用户。'在 Html 中的每个模型前面
ng-model="user.tipPercent"
ng-model="user.mealTax"
ng-model="user.mealPrice"
// $scope.ready= false;
$scope.mealPrice ="" ;
$scope.mealTax = 0.05;
$scope.tipPercent =0.05;
// possibly could do
var defaultForm={
price: "",
tax: "",
tip:""
}
$scope.cancel = function() {
$scope.myForm.$setPristine();
$scope.user = angular.copy(defaultForm);
console.log('empty');
}
var defaultForm = {
mealPrice : "",
mealTax : 0.05,
tipPercent : 0.05
}
$scope.user = angular.copy(defaultForm);
$scope.cancel = function () {
$scope.myForm.$setPristine();
$scope.user = angular.copy(defaultForm);
console.log('empty');
}
关于angularjs - $setPristine() 不工作。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27191744/
我正在尝试创建一个基本的登录表单,当加载页面并提交表单时,表单将重置。 我不断收到以下错误: 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()
我是一名优秀的程序员,十分优秀!