2016-03-31 19 views
0

パスワードを比較し、パスワードフィールドを確認する指示を作成し、一致しない場合はエラーメッセージを表示します。Angularjs ngMessageが表示されない

(function() { 
    'use strict'; 
    var compareTo = function() { 
     return { 
      require: "ngModel", 
      scope: { 
       otherModelValue: "=compareTo" 
      }, 
      link: function (scope, element, attributes, ngModel) { 

       ngModel.$validators.compareTo = function (modelValue) { 
        return modelValue == scope.otherModelValue; 
       }; 

       scope.$watch("otherModelValue", function() { 
        ngModel.$validate(); 
       }); 
      } 
     }; 
    }; 
    angular.module('StarterApp').directive("compareTo", compareTo); 

})(); 

マイHTML:

    <form name="updatePwdForm" novalidate> 
         <md-input-container class="md-block"> 
          <label for="password">Password:</label> 
          <input type="password" name="password" ng-model="ctrl.updatepassword.password" /> 
         </md-input-container> 

         <md-input-container class="md-block"> 
          <label for="confirmPassword">Confirm Password:</label> 
          <input type="password" name="confirmPassword" label="confirmPassword" ng-model="ctrl.updatepassword.confirmpassword" required 
          compare-to="ctrl.updatepassword.password" /> 
          <div ng-messages="updatePwdForm.confirmPassword.$error" style="color:maroon" role="alert"> 
           <div ng-message="required">Password and Confirm Password are not same!</div>         
          </div> 
         </md-input-container> 

         <md-button class="md-raised md-primary" ng-disabled="updatePwdForm.$invalid" ng-click="ctrl.updatePassword()">Update</md-button> 

        </form> 
       </md-content> 

私は、私のディレクティブは、値を比較すると、私のボタンは、まだ私は正しいconfirmpasswordを入力ディスエーブルモードになっているので、それが一致しない場合はfalseを返していることを確信しているが、私のngMessageは表示されません。 何か不足していますか? ありがとうございます。あなたのバリデータの名前がcompareToあるので、代わりに

<div ng-message="required"> 

答えて

1

あなたは

<div ng-message="compareTo"> 

を持つ必要があります。

+0

恐ろしい@JB Nizet、ありがとう。 – Venkat

関連する問題