2016-10-18 3 views
0

내 페이지를 열면 메시지가 거품 이벤트처럼 출력됩니다. Message 2, Message 1. 그러나 터널 이벤트처럼 Message 1, Message 2 출력이 필요합니다. 왜 그렇게 된거야?각도 지시어 dom 빌드

<div by-global-size="width:100%;height:5%;"> 
<div by-parent-size="width:50%;height:30%;"> 
</div> 
</div> 

.directive("byGlobalSize", function() { 
    return { 
     link: function (scope, element, attributes) { 
      alert("Message 1"); 
     }, 
     restrict: "A", 
     scope: true 
    } 
}) 
.directive("byParentSize", function() { 
    return { 
     link: function (scope, element, attributes) { 
      alert("Message 2"); 
     }, 
     restrict: "A", 
     scope:true 
    } 
}); 

두 개의 지시문이 없어도 하나의 예입니다.

<div by-global-size="width:100%;height:5%;"> 
<div by-parent-size="width:50%;height:30%;"> 
<div by-parentChild-size="width:100%;height:5%;"> 
<div by-parentChildLittle-size="width:50%;height:30%;"> 
</div> 
</div> 
</div> 
</div> 

하나 더 HTML 예제. 부모부터 자식까지, 나는 dom 트리를 만들어야합니다. 계층 구조가 어떻게 든이있는 경우

답변

0

확인 다음 예를 plunker에서. 컴파일 함수는 사전 컴파일과 포스트 컴파일의 두 가지 함수를 사용합니다.

directive('myDirective', function myDirective($log){ 
    var directive = { 
    retstrict : 'A', 
    compile : function compile(){ 
     return { 
      pre : function(scope, iElem, iAttr){ 
       //Your code goes here 
      }, 
      post : function(scope, iElem, iAttr){ 
       // in essence the link function 
      } 
     } 
    } 

    }; 
    return directive; 
} 

https://plnkr.co/edit/zEk0icn4KaQFtX0WHonj?p=preview

링크 기능

는 본질적으로 포스트 컴파일 기능입니다. 당신이 필요로하는 것은 outter에서 inner로 작업하는 pre-compile 방법입니다.

+0

Build result, inner -> outter,하지만 outter -> inner가 필요합니다. 그가 처음에 initliazed하지 않으면 내면의 크기 부모에 대해 어떻게 알게 될까요? ... –

+0

미안하지만, 그것은 일이라고 생각합니다. 예 ... 예. 내가 너에게 말한 작품이라면 나는 밤에 프로젝트에서 노력할 것이다. 고마워. 나는 아주 빨리 대답한다. –

+0

감사합니다. 사전 컴파일 작업이 필요합니다! –

0

, 당신은 당신의 지시에 require 키워드를 사용한다 :

.directive("byParentSize", function() { 
    return { 
     link: function (scope, element, attributes) { 
      alert("Message 2"); 
     }, 
     restrict: "A", 
     scope:true, 
     require:'^byGlobalSize', 
    } 
}); 
+0

모든 지시어에 필요합니다. 이 두 지시어뿐만 아니라 ... 그것은 단지 예일뿐입니다. –