2016-09-25 46 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)範圍將使其與父級範圍緊密結合。

標記

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

指令

scope: { 
    phoneNumber: '=phoneType', 
    somethingMore: '=' //pass more data here 
}