重置表单字段时出错

Error in resetting form field

本文关键字:出错 字段 表单      更新时间:2023-09-26

我有一个模式窗口,用户在其中保存数据,所以在下面,每次用户打开模式窗口时,我都将表单字段重新设置为空白,它都按预期工作,但angularjs表单验证消息由于脏检查而填充,现在我添加了$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>

main.js

 $scope.addMessageNotification = function() {
        clearNotificationForm();
        $scope.editMessagesForm.$setPristine();
        $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 = '';
    };

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();
};

错误

 Cannot read property '$setPristine' of undefined

---------------------------------------------更新------------------------------------------

dudeee我已经把你的整个表单放在更新的plunker中了,你为什么希望$setPristine触发?当你没有触发它时。

您首先犯了两个错误,需要将var clearNotificationForm = function ()绑定到$scope,如下所示,变量不足以将函数绑定到您的$scope

$scope.clearNotificationForm = function()
{
    // All your stuff to above $setPristine goes here
}

第二,我看不到你在表单中的任何地方触发上述功能,你需要像我在更新的plunker中那样,通过按钮触发它,你会在更新结束时找到链接。

<button class="button" ng-click="reset()">Reset</button>

在您的情况下,一旦您将功能绑定到$scope,您的按钮将如下所示:

<button class="button" ng-click="clearNotificationForm ()">Reset</button>

在您的情况下,函数名称如下,但您需要首先将其绑定到$scope

$scope.clearNotificationForm = function()
{
} 

如果你觉得我的帖子有帮助,请给我一个赞,我将不胜感激。

这是更新的Plunker


-------------------------------------第一反应-----------------------------------

首先,确保您使用与以下相同的ng模型命名输入类型

<input name="name1" ng-model="messageNotificationDTO.adminNotificationTypeCode" placeholder="Name" required/>

您需要用$scope定义父对象messageNotificationDTO,然后在其内部定义子对象adminNotificationTypeCode

例如:

$scope.messageNotificationDTO = 
{ 
    "adminNotificationTypeCode": "",
    "adminNotificationName": ""
};

将所有对象设置为空,然后重置表单,如:

$scope.reset = function()
{  
  $scope.messageNotificationDTO.adminNotificationTypeCode = "";
  $scope.messageNotificationDTO.adminNotificationName  = "";
  $scope.form.$setPristine();
}

下面是一个由控制器代码生成的工作示例。

plunker