2016-09-25 4 views
0
.directive('phoneType', [function() { 
    return { 
     scope: { 
      phoneNumber: '=phoneType' 
     } 
     link: function (scope, element, attrs) { 
      // now do stuff with the number, you can access it through the scope 
      scope.phoneNumber // contains the number 
      scope.vm.somethingInMyController // vm is undefined 
     } 
    }; 
}]) 

에 부모와 요소의 범위를받는 방법 어떻게 달성 할 모두 phoneType 내 지시에 vm 변수?각도 : 지시어

+0

CONSOLE.LOG 범위를보십시오. $ 부모. –

답변

1

격리 된 범위가되도록 지시하려면 해당 특성을 통해서만 해당 속성의 scope으로 필요한 데이터를 전달해야합니다. 지시어에서 parent ($ parent를 사용하여) 스코프에 액세스하면 명명 규칙을 따를 경우 부모 범위와 밀접하게 연결됩니다.

마크 업

<div phone-type 
    phone-number="vm.myPhoneNumber" 
    something-more="vm.somethingInMyController"> 
</div> 

지침

scope: { 
    phoneNumber: '=phoneType', 
    somethingMore: '=' //pass more data here 
} 
관련 문제