2013-11-03 2 views
0

templateUrl 및 링크 속성을 사용하여 DDO를 반환하는 지시문이 있습니다. 내 templateUrl에 체크 상자 요소가 포함 된 div에 ngRepeat 지시문이 있습니다. 내 링크 함수에서 모든 자식 체크 박스를 선택하려하지만 내 링크 함수가 호출 될 때 DOM에 추가되지 않습니다. 체크 박스를 $ timeout에 포장하면 당연히 선택됩니다. 문서에 따라 각도는 다음과 같은 순서로 함수를 호출합니다.링크 함수가 호출 될 때 요소가 DOM에 없습니다?

mainDirective-> 컴파일 -> preLink -> firstChildDirective-> 컴파일 -> preLink -> lastChildDirective-> 컴파일 -> preLink-> postLink -> firstChildDirective-> postLink -> mainDirective -> postLink.

Angular docs에 따르면 DDO를 링크 속성으로 반환하면 postLink로 호출됩니다. 이는 내 생각에 모든 하위 체크 박스가 이미 DOM에 있어야하지만 이는 사실이 아닙니다.

각 코드를 살펴보면 나는 당신이 컴파일 기능을 가지고 있지 않을 때이 templateUrl 경우라고 볼 수

또한
if (directive.templateUrl) { 
     assertNoDuplicate('template', templateDirective, directive, $compileNode); 
     templateDirective = directive; 
     nodeLinkFn = compileTemplateUrl(directives.splice(i, directives.length - i), 
      nodeLinkFn, $compileNode, templateAttrs, jqCollection, directive.replace, 
      childTranscludeFn); 
     ii = directives.length; 
    } else if (directive.compile) { 
     try { 
     linkFn = directive.compile($compileNode, templateAttrs, childTranscludeFn); 
     if (isFunction(linkFn)) { 
      addLinkFns(null, linkFn); 
     } else if (linkFn) { 
      addLinkFns(linkFn.pre, linkFn.post); 
     } 
     } catch (e) { 
     $exceptionHandler(e, startingTag($compileNode)); 
     } 
    } 

이 링크 속성이며 templateUrl의 directive.compile가있는 경우 할당 된 지시문. 링크.

addLinkFns (null, linkFn)는 templateUrl이 없을 때 postLink에 링크 함수를 바인딩하지만 일부 자식 지시문에 templateUrl이 있으면 어떻게되는지 분명히 알 수 있습니다. 링크 기능이 호출 될 때 하위 체크 박스를 사용할 수없는 이유는 무엇입니까? 도움 주셔서 감사합니다 !!!

+0

'ng-repeats'는 익숙하지 않은 경우 펑키합니다. 요소의 DOM (digest cycle completed)이 완전히 완료되기 전에 지시문의'link '가 시작되는 것 같습니다. 그래서 당신은'$ timeout()'에 DOM 조작 코드를 래핑 할 수있다. 지연을 필요로하지 않는다. 단지 사이클 스택의 마지막에 놓이게된다. – charlietfl

답변

0

한 가지를 잊어 버리십시오. 지시어는 정적 일 필요는 없습니다. 그들은 모델의 변화에 ​​반응 할 수 있습니다. 그게 ng-repeat입니다. 요소는 모델이 평가 될 때 생성/제거됩니다. 예를 들어, 런타임에 항목을 추가 할 수 있습니다. 그러면 지시문이 링크되어있을 때 확인란이 DOM에있을 가능성은 거의 없습니다.

관련 문제