我学习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'属性吗? –