我學習AngularJS指令AngularJS指令,並有一件事我想要做的是通過在父scope
(一controller
的scope
)一些變量$scope.message
,我希望它被重命名爲param
裏面的directive
alert
。我可以用一個孤立的範圍做到這一點:傳遞變量沒有隔離範圍
<div alert param="message"></div>
,並定義
.directive("alert", function(){
return{
restrict: "A",
scope: {
param: "="
},
link: function(scope){
console.log(scope.param) # log the message correctly
}
}
})
但我可以做到這一點,而無需使用隔離範圍是什麼?假設我想另一個directive
toast
添加到<div toast alert></div>
並使用相同的param
(保持2路數據綁定),天真的我會做
.directive("toast", function(){
return{
restrict: "A",
scope: {
param: "="
},
link: function(scope){
console.log(scope.param)
}
}
})
我一定會得到一個錯誤Multiple directives [alert, toast] asking for new/isolated scope on:<div...
所以總之,我的問題是,如何在沒有隔離範圍的情況下重命名父範圍變量,以及如何在兩個directives
放置在單個DOM
上時共享變量?
你想要2個指令綁定到相同的父'$ scope'屬性嗎? –