2017-07-30 91 views
1

我想在用戶單擊提交表單後顯示驗證消息。 例如文本框是必需的,用戶直接點擊提交按鈕,然後應顯示驗證字段。在提交Angularjs表單時顯示驗證消息

這裏當用戶點擊表單時什麼都不會發生。

<body ng-controller="AppController as ctrl"> 

<form ng-submit="ctrl.submit()" name="myForm" novalidate> 
    <input type="text" ng-model="ctrl.user.username" name="uname" class="username" placeholder="Enter your name" required ng-minlength="3" /> 
    <span ng-show="myForm.$dirty && myForm.uname.$error.required">This is a required field</span> 

    <input type="text" ng-model="ctrl.user.address" placeholder="Enter your Address" /><br /><br /> 

    <input type="email" ng-model="ctrl.user.email" name="email" class="email" placeholder="Enter your Email" required /> 
    <span ng-show="myForm.$dirty && myForm.email.$error.required">This is a required field</span> 

    <input type="submit" value="Submit"> 
</form> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js"> 
</script> 
<script> 
    angular.module('myApp', []) 
    .controller('AppController', [function() { 
     var self = this; 
     self.submit = function() { 
      console.log('Form is submitted with following user', self.user); 
     }; 
    }]); 
</script> 

回答

2

角時提交表單,使其在形式層面$submitted屬性true,也把它添加上form的類屬性ng-submitted類。

你可以使用窗體對象的$submitted財產像myForm.$submitted鑑定表已提交或不

ng-show="myForm.$submitted && myForm.uname.$error.required" 

Demo Plunker

+0

謝謝,我正在學習角1你可以建議一些有用的鏈接。 – Dave

相關問題