我正在学习AngularJS指令,我想做的一件事就是传递一些变量 $scope.message
在父母 scope
(一个 scope
一个 controller
),我想将它重命名为 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
(保持双向数据绑定),天真地我会这样做
.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
?