show validation error messages on submit in angularjs

前端 未结 13 865
别那么骄傲
别那么骄傲 2020-11-28 02:25

I have a form which need to show validation error messages if clicked submit.

Here is a working plunker

 
13条回答
  •  无人及你
    2020-11-28 02:36

    There are two simple & elegant ways to do it.

    Pure CSS:

    After first form submission, despite the form validity, Angular will add a ng-submitted class to all form elements inside the form just submitted.

    We can use .ng-submitted to controller our element via CSS.

    if you want to display an error text only when user have submitted e.g.

    .error { display: none }
    .ng-submitted .error {
         display: block;
    }
    

    Using a value from Scope:
    After first form submission, despite the form validity, Angular will set [your form name].$submitted to true. Thus, we can use that value to control elements.

    error message

提交回复
热议问题