2013-09-30 3 views
22
link: function(scope, elm, attrs, ctrl) { 
     ctrl.$parsers.unshift(function(viewValue) { 

      scope.pwdValidLength = (viewValue && viewValue.length >= 8 ? 'valid' : undefined); 
      scope.pwdHasLetter = (viewValue && /[A-z]/.test(viewValue)) ? 'valid' : undefined; 
      scope.pwdHasNumber = (viewValue && /\d/.test(viewValue)) ? 'valid' : undefined; 

      if(scope.pwdValidLength && scope.pwdHasLetter && scope.pwdHasNumber) { 
       ctrl.$setValidity('pwd', true); 
       return viewValue; 
      } else { 
       ctrl.$setValidity('pwd', false);      
       return undefined; 
      } 

     }); 
    } 

http://jsfiddle.net/adamdbradley/Qdk5M/

Dans le violon mentionné ci-dessus comment la validation du mot de passe en cours? Que fait le $ parser.unshift ?? et quelle est l'utilisation de test (viewValue) .....? J'ai parlé AngularJS site principal mais ne pouvait pas comprendre quelque chose ... S'il vous plaît me guider pas à pas de la façon dont il est ... valider

Je suis nouveau à AngularJS ..

Répondre

60

Voici une explication étape par étape. Notez que la documentation est vraiment bonne: les pages sur the forms et sur the $parsers sont celles que vous cherchez.

link: function(scope, elm, attrs, ctrl) { 
    /** 
    * This function is added to the list of the $parsers. 
    * It will be executed the DOM (the view value) change. 
    * Array.unshift() put it in the beginning of the list, so 
    * it will be executed before all the other 
    */ 
    ctrl.$parsers.unshift(function(viewValue) { 

     scope.pwdValidLength = (viewValue && viewValue.length >= 8 ? 'valid' : undefined); // Check the length of the string 
     scope.pwdHasLetter = (viewValue && /[A-z]/.test(viewValue)) ? 'valid' : undefined; // Check if the string contains letter. RegExp.test() simply returns a boolean if the string matches the regex. 
     scope.pwdHasNumber = (viewValue && /\d/.test(viewValue)) ? 'valid' : undefined; // Check if the string contains digit. Same remark. 

     if(scope.pwdValidLength && scope.pwdHasLetter && scope.pwdHasNumber) { // If all is good, then… 
      ctrl.$setValidity('pwd', true); // Tell the controlller that the value is valid 
      return viewValue; // Return this value (it will be put into the model) 
     } else { // … otherwise… 
      ctrl.$setValidity('pwd', false); // Tell the controlller that the value is invalid 
      return undefined; // When the value is invalid, we should return `undefined`, as asked by the documentation 
     } 

    }); 
} 
+1

Grande réponse. Devrait être accepté. – hitokiri82

+0

Juste pour être clair, $ parsers est un tableau (ce qui signifie, type javascript standard, pas spécifique à Angular). unshift est une méthode native au prototype Javascript Array, ainsi le commentaire dans cette réponse de "Array.unshift() le met au début de la liste ..." C'est une distinction importante, je pense, parce que les débutants peuvent être confus entre ce qui est angulaire et ce qui est natif JS. – dudewad