.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 :(得分:1)
由于您希望指示具有隔离范围,因此只应通过其属性将所需数据传递到指令scope
属性。从指令访问父(使用$ parent)作用域将使其与父作用域紧密结合,假设有一些命名约定。
<强>标记强>
<div phone-type
phone-number="vm.myPhoneNumber"
something-more="vm.somethingInMyController">
</div>
<强>指令强>
scope: {
phoneNumber: '=phoneType',
somethingMore: '=' //pass more data here
}