2014-06-11 2 views
0

이것은 정규식으로 간단한 검증을 수행하는 지시어입니다. 어떤 이유로 나는 지시문 바깥에있는 체크 박스 값을 전달하는 문제가있다. 나는 checkEmailOnBlur라는 내 지시에 scope.validationEnabled라고 체크 박스 값을 읽기 위해 노력하고 있어요 :범위 체크 박스 값이 지시어에 전달되지 않았습니까?

app.directive('checkEmailOnBlur', function() { 
    var ABN_REGX = /^(\d *?){3}$/; 
    return { 
    restrict: 'A', 
    require: 'ngModel', 
    link: function(scope, elm, attr, ctrl) { 

     if (scope.validationEnabled) { 

     if (attr.type === 'radio' || attr.type === 'checkbox') return; 
     elm.unbind('input').unbind('keydown').unbind('change'); 

     elm.bind('blur', function() { 
      scope.$apply(function() { 
      if (ABN_REGX.test(elm.val())) { 
       ctrl.$setValidity('abns', true); 
       console.log('valid abn'); 
       scope.acn = "1010101"; 
       scope.displayName = "display name"; 
       scope.otherName = "otherName"; 
       scope.description = "desc"; 

      } else { 
       ctrl.$setValidity('abns', false); 
       console.log('not valid abn'); 
       scope.acn = ""; 
       scope.displayName = ""; 
       scope.otherName = ""; 
       scope.description = "" 
      } 
      }); 
     }); 
     } 
    } 
    }; 

질문 :이 확인란을 선택하면 순간에 scope.validationEnabled에 대한 TRUE로 설정되어 있지 않습니다. 체크 박스 값을 어떻게 전달할 수 있습니까? 링크 기능을 실행하면 http://plnkr.co/edit/O69SP2sB5NZcPirImhrh?p=preview

답변

0

scope.validationEnabled이 경우 falsy이다 undefined됩니다 여기 plnkr 기준이다. 링크 함수는 이벤트 리스너를 blur에 연결하지 않습니다.

다음 번에 HTML을 컴파일 할 때까지 링크 기능이 다시 실행되지 않으므로 확인란을 선택해도 문제가되지 않습니다.

elm.bind('blur', function() { 

    if (!scope.validationEnabled) return; 

    // Code 

데모 :http://plnkr.co/edit/qS5YP546qX8I9QwIwupz?p=preview

이벤트 리스너 함수에 if 이동

관련 문제