2015-09-18 67 views
0

enter image description here我想顯示json對象中所有字段的值。我能夠在對象中添加名字,電子郵件,密碼。但是我的確認密碼不顯示在對象中爲什麼?我輸入相同的密碼以確認密碼仍然沒有顯示如何在角js中顯示json合成器中的對象?

這裏是我的代碼

http://plnkr.co/edit/iHA8iQC1HM5OzZyIg4p3?p=preview

angular.module('app', ['ionic','ngMessages']).directive('compareTo',function(){ 



    return { 
     require: "ngModel", 
     scope: { 
      otherModelValue: "=compareTo" 
     }, 
     link: function(scope, element, attributes, ngModel) { 

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

      scope.$watch("otherModelValue", function() { 
       ngModel.$validate(); 
      }); 
     } 
    }; 

爲什麼確認密碼不顯示?

}).controller('first',function($scope){ 

}) 

回答

0

您的compareTo指令失敗,如果驗證程序失敗,它將不會綁定到模型。如果您從代碼中刪除compareTo指令,您將在您的作用域中獲得confiredpassword。

請參考:password-check directive in angularjs來修復您的comparTo指令。

而且這裏是固定指令的plunker: http://plnkr.co/edit/wM3r6eR2jhQS7cjvreLo?p=preview

.directive('compareTo', function() { 
     return { 
      scope: { 
       targetModel: '=compareTo' 
      }, 
      require: 'ngModel', 
      link: function postLink(scope, element, attrs, ctrl) { 

       var compare = function() { 

        var e1 = element.val(); 
        var e2 = scope.targetModel; 

        if (e2 !== null) { 
         return e1 === e2; 
        } 

        return false; 
       }; 

       scope.$watch(compare, function(newValue) { 
        ctrl.$setValidity('errorCompareTo', newValue); 
       }); 

      } 
     };