2015-09-18 68 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); 
       }); 

      } 
     };