Javascript 重置表单字段时出错
我有一个模式窗口,用户在其中保存数据,因此在每次用户打开模式窗口时,我都会将表单字段重置为空白,但由于脏检查,angularjs表单验证消息正在填充。现在,我添加了Javascript 重置表单字段时出错,javascript,angularjs,validation,angularjs-scope,Javascript,Angularjs,Validation,Angularjs Scope,我有一个模式窗口,用户在其中保存数据,因此在每次用户打开模式窗口时,我都会将表单字段重置为空白,但由于脏检查,angularjs表单验证消息正在填充。现在,我添加了$setPristine(),但它抛出错误$setPristine()未定义 main.html <form id="editMessagesForm" name="editMessagesForm" novalidate class="border-box-sizing"> <div class="moda
$setPristine()
,但它抛出错误$setPristine()
未定义
main.html
<form id="editMessagesForm" name="editMessagesForm"
novalidate class="border-box-sizing">
<div class="modalForm">
<div class="modalBorder">
<div class="row" ng-if="messageNotificationDTO.adminNotificationKey">
<div class="form-group col-md-12 fieldHeight">
<label class="col-md-4">Message Key:</label>
<div class="col-md-8">
<input type="text" class="form-control" id="messageNotificationKey"
name="messageNotificationKey"
ng-model="messageNotificationDTO.adminNotificationKey"
disabled="disabled">
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-12 fieldHeight">
<label class="col-md-4">Message Type:</label>
<div class="col-md-8">
<select name="mesgLookUpcode" class="form-control"
ng-model="messageNotificationDTO.adminNotificationTypeCode" required
id="mesgLookUpcode"
ng-options="adminDataSource.id as adminDataSource.text for adminDataSource in adminDataSource">
<option value="">Select...</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-12 fieldHeight">
<label class="col-md-4 required">Notification Name:</label>
<div class="col-md-8">
<textarea rows="2" class="form-control"
ng-model="messageNotificationDTO.adminNotificationName"
name="adminNotificationName"
required
id="adminNotificationName" txt-area-maxlen="128"
ng-disabled="readOnlyFlag"
data-tooltip-html-unsafe="<div>{{128 - messageNotificationDTO.adminNotificationName.length}} characters left</div>"
tooltip-trigger="{{{true: 'focus', false: 'never'}[messageNotificationDTO.adminNotificationName.length >= 0 || messageNotificationDTO.adminNotificationName.length == null ]}}"
tooltip-placement="top" tooltip-class = "bluefill"></textarea>
<p class="text-danger" ng-show="editMessagesForm.adminNotificationName.$touched && editMessagesForm.adminNotificationName.$error.required">Message Notification Name is required</p>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer footerMargin">
<button type="button" class="btn btn-primary pull-right mousedwncall"
ng-click="saveMessages()" ng-disabled="editMessagesForm.$invalid"
ng-class="{disableSaveCls:editMessagesForm.$invalid}"
>Save</button>
<button class="btn btn-default pull-left mousedwncall"
ng-click="handleCancelMessageModal()">Cancel</button>
</div>
</form>
ctrl.js
$scope.messageNotificationDTO = {
adminNotificationTypeCode: [],
adminNotificationName:'',
adminNotificationBody: '',
adminNotificationStartTime: '',
adminNotificationEndTime: '',
activeFlag: ''
};
$scope.adminDataSource = adminData.data;
//Set notification grid config and dataSource
$scope.messageGridOptions = messageGridConfig.messagesGridOptions;
messageGridConfig.messagesGridOptions.dataSource = MessageAdminNotificationFactory.getNotificationDataSource();
//Save Notification
$scope.saveMessages = function() {
MessageAdminNotificationFactory.saveMessageAdmin($scope.messageNotificationDTO).then(function() {
$scope.messageNotificationModal.close();
$scope.messageGridOptions.dataSource.read();
clearNotificationForm();
});
};
//Add new Notification
$scope.addMessageNotification = function() {
$scope.messageNotificationModal.open().center();
};
var clearNotificationForm = function () {
$scope.messageNotificationDTO.adminNotificationTypeCode = [];
$scope.messageNotificationDTO.adminNotificationName = '';
$scope.messageNotificationDTO.adminNotificationBody = '';
$scope.messageNotificationDTO.adminNotificationStartTime = '';
$scope.messageNotificationDTO.adminNotificationEndTime = '';
$scope.messageNotificationDTO.activeFlag = '';
$scope.editMessagesForm.$setPristine();
};
错误
------------------------------------------更新------------------------------------------
dudeee我已经把你的整个表单放在更新的plunker中了为什么你希望触发$setPristine
?当你没有触发它的时候
您犯了两个错误,首先您需要将var clearNotificationForm=function()
绑定到$scope
,如下所示,变量不足以将函数绑定到$scope
$scope.clearNotificationForm = function()
{
// All your stuff to above $setPristine goes here
}
$scope.clearNotificationForm = function()
{
}
其次,我看不到您在表单中的任何位置触发上述函数,您需要在按钮的帮助下触发它,就像我在更新的plunker中所做的那样,您将在更新结束时找到链接
<button class="button" ng-click="reset()">Reset</button>
在您的情况下,函数名如下所示,但您需要首先将其绑定到$scope
$scope.clearNotificationForm = function()
{
// All your stuff to above $setPristine goes here
}
$scope.clearNotificationForm = function()
{
}
如果你觉得我的帖子有帮助,请给我一个类似的,我将非常感谢
以下是最新的
-------------------------------------第一反应----------------------------------- 首先,请确保使用与以下相同的ng模型命名输入类型
<input name="name1" ng-model="messageNotificationDTO.adminNotificationTypeCode" placeholder="Name" required/>
将所有对象设置为空,然后重置窗体,如下所示:
$scope.reset = function()
{
$scope.messageNotificationDTO.adminNotificationTypeCode = "";
$scope.messageNotificationDTO.adminNotificationName = "";
$scope.form.$setPristine();
}
下面是一个由控制器代码生成的工作示例
什么是$setPristine()?您也可以发布html吗?您的代码看起来是正确的。我认为您的表单名称有误或其他问题……发布您的HTML代码……或提供JSFIDLE或plnkr@WasiqMuhammad我实现了您的逻辑,但它仍然抛出未定义的setPristine,添加了有问题的代码我正在触发saveMessages方法中的
clearNotificationForm(),我们不需要为重置添加额外的按钮,我相信请让我将您的确切代码放在plunker上,以检查此问题是否是因为上面提到的来自ur main.js或ctrl.jsI的更新代码引起的。我只是阅读angularjs文档,发现表单名称必须是对象属性,如vm.formname,我实现了这一点,它起了作用,但感谢您的帮助,您的回答也让我了解了angularjs验证。
$scope.messageNotificationDTO =
{
"adminNotificationTypeCode": "",
"adminNotificationName": ""
};
$scope.reset = function()
{
$scope.messageNotificationDTO.adminNotificationTypeCode = "";
$scope.messageNotificationDTO.adminNotificationName = "";
$scope.form.$setPristine();
}