2014-03-06 116 views
1

我是Angular的新手,我嘗試做一個簡單的黑名單檢查。目前,我有兩個文本可以用ng-show顯示和隱藏。當郵件模式錯誤時應顯示第一個,當正確和/或在黑名單上時隱藏。電子郵件黑名單和驗證

我的問題是,我不知道模型必須如何實現。目前它是由複選框模擬的。也許有人有提示。

<div class="controls"> 
    <input type="checkbox" ng-model="checked" ng-init="checked=true" /><br/> 
    <input type="email" id="inputEmail" placeholder="Email" ng-model="email" required> 
<div class="hint"> 
    <h4 name="mailValidator" ng-if="checked" ng-show="true">Invalid Email</h4> 
    <h4 name="checkBlacklist" ng-if="!checked" ng-show="true">Email is not allowed</h4> 
</div> 

Here is a Fiddle-Demo

回答

0

我爲您的問題創建JSFiddle。

JSFiddle

查看:

<div ng-controller="MyCtrl"> 
    <input placeholder="Email" ng-model="email" ng-pattern="emailRegExp" ng-class="{ error: !email }" /> 
    <h4 name="mailValidator" ng-show="!email">Invalid Email</h4> 
    <h4 name="checkBlacklist" ng-show="email && inBlackList">Email is not allowed</h4> 
</div> 

控制器:

function MyCtrl($scope) { 
    $scope.inBlackList = false; 

    //get from DB in your case 
    var bannedEmail = ['[email protected]','[email protected]','[email protected]'] 

    $scope.emailRegExp = /^[A-Za-z0-9._%+-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/; 
    $scope.$watch('email', function(newValue) { 
     if(newValue){ 
      if(bannedEmail.indexOf(newValue) > -1) { 
       $scope.inBlackList = true; 
      } 
      else { 
       $scope.inBlackList = false; 
      } 
     } 
    });  
} 
0

如果你使用AngularJS forms驗證你可以爲第一個文本做

<form name="form" class="css-form" novalidate> 
    <div class="controls"> 
     <input type="checkbox" ng-model="checked" ng-init="checked=true" /><br/> 
     <input type="email" id="inputEmail" placeholder="Email" ng-model="email" required> 
    </div> 
    <div class="hint"> 
     <h4 name="mailValidator" ng-show="form.uEmail.$error.email && !onBlacklist">Invalid Email</h4> 
     <h4 name="checkBlacklist" ng-show="onBlacklist">Email is not allowed</h4> 
    </div> 
</form> 

然後在控制器,你可以看電子郵件的變化,當它被列入黑名單設置onBlacklist爲true,希望幫助