2013-07-04 24 views
17

輸入類型=「email」和ng-model屬性有什麼特別的嗎? 如果輸入是電子郵件,則模型不會更新。 如果我將輸入類型更改爲正確更新的文本,數字或日期。是否有ng模型和輸入類型的電子郵件錯誤?

錯誤或一些特殊的魔法電子郵件驗證行爲,我不明白?

+0

也許你的意思是這個http://www.youtube.com/watch?v=ZhfUv0spHCY&t=29m19s? –

+0

您正在使用哪個版本的角度 –

回答

29

它對輸入進行了一些驗證,因此您需要在綁定到模型之前輸入有效的電子郵件地址。

這是所使用的正則表達式:

/^[A-Za-z0-9._%+-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/ 

基本上你需要輸入至少是[email protected]

3

補體的地址,你可以使用你的屬性構成,看看你的電子郵件是有效的,就像這樣:

HTML

<form name="myForm" ng-submit="submit()"> 
    <input type="email" ng-model="email1" name="email1" /> 
</form> 

的Javascript

//[formName].[inputFieldName].property 
myForm.email1.$pristine; 
// Boolean. True if the user has not yet modified the form. 
myForm.email1.$dirty 
// Boolean. True if the user has already modified the form. 
myForm.email1.$valid 
// Boolean.True if the the form passes the validation. 
myForm.email1.$invalid 
// Boolean. True if the the form doesn't pass the validation. 
myForm.email1.$error 

Reference

2

從角1.3開始,你可以很容易地覆蓋了 '電子郵件' 驗證,並使其始終返回true。

angular 
 
    .module('myApp', []) 
 
    .controller('MainController', function() { 
 
    this.email = ''; 
 
    }) 
 
    .directive('noEmailValidation', function() { 
 
    return { 
 
     restrict: 'A', 
 
     require: 'ngModel', 
 
     link: function(scope, elm, attr, ctrl) { 
 
     ctrl.$validators['email'] = function() { 
 
      return true; 
 
     }; 
 
     } 
 
    } 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script> 
 
<div ng-app="myApp"> 
 
    <form ng-controller="MainController as main"> 
 
    <div>Email: {{main.email}}</div> 
 
    <input type="email" ng-model="main.email" no-email-validation> 
 
    </form> 
 
</div>

享受。

11

這不是一個錯誤,只有當我們輸入正確的電子郵件地址格式進行電子郵件驗證時,它纔會更新。 添加此屬性ng-model-options="{'allowInvalid': true}"以允許輸入無效的電子郵件。

相關問題