簡體   English   中英

如果用戶在提交后從文本框中刪除了文本,則僅在提交后應用角度驗證,而不顯示驗證

[英]apply angular validations only after submit and not to show validations, if user removed text from text box after submit

如果用戶在提交后從文本框中刪除了文本,則僅在提交后應用角度驗證,而不顯示驗證。 從技術上講,我的要求是->如果用戶輸入了文本,則不應顯示任何驗證->僅在用戶單擊“提交”后才顯示驗證&->在用戶觸摸該文本框(或)從文本框中刪除文本后提交然后驗證消息應該不顯示

你們能給我解決方案,謝謝您的答復,如果您提供給我小提琴,我將非常感謝。

提前致謝

我不確切知道您在尋找什么,但是我想這段代碼可能會有用。

它只是具有其控制器和一些經過驗證的字段的基本形式。 在此示例中,每個字段都是必填字段,但是您可以看到沒有驗證反饋提供給用戶。

僅當表單有效時驗證才通過,然后使用標志(true / false)注冊提交。 如果標記為true,並且用戶觸摸了其中一個字段,則所有字段再次為空白。

控制器

(function(){


    var Controller = function($scope){

        // Parameters
        var isFormSubmitted = false;

        // Methods

        // Shared Methods
        $scope.checkFocus = function(){

            if(!isFormSubmitted){ 
                return;
            }

            // Reset all fields
            $scope.fields = null;

            // Do something

        }

        $scope.validate = function(){

            isFormSubmitted = true;

            // Do some validation

        };

    };

    Controller.$inject = [
        '$scope'
    ];

    app.controller('MainCtrl', Controller);


})();

風景

<div ng-controller="MainCtrl">


   <form name="form" ng-submit="form.$valid && validate()" novalidate>

        <input ng-focus="checkFocus()" ng-model="fields.username" type="text" required placeholder="Username">
        <input ng-focus="checkFocus()" ng-model="fields.password" type="password" required placeholder="Password">
        <input type="submit" value="Submit">

   </form>


</div>

您可以使用標志來解決問題。 假設您有一個包含輸入的表單。 單擊提交按鈕時,必須將標志isSubmitted設置為true。 並在輸入更改時將其設置為false。 然后根據該標志顯示您的驗證消息:這是一個假設的控制器:

function appCtrl($scope) {
  $scope.isSubmitted = false;

  $scope.submit = function() {
    $scope.isSubmitted = true;
    //...
  }

  $scope.inputChanged = function() {
    $scope.isSubmitted = false;
  }
}

這可能是您觀點的一部分:

  <input name="inputName" type="text" ng-model="myModel" ng-change="inputChanged()" required="" />
  <span ng-show="form.$error.required && form.$dirty && isSubmitted">name is required</span>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM