如何防止指令與transclude創建新的範圍?AngularJS指令transclude scope = false?
This jsfiddle我不能綁定任何東西,因爲用紅色邊框說明的新範圍。
HTML:
<div ng-app="components">
<input ng-model="var">
<block>
123
<input ng-model="var">
</block>
</div>
的JavaScript:
angular.module('components', []).directive('block',function(){
return{
scope:false,
replace:true,
restrict:"E",
transclude:true,
template:'<div class="block" ng-transclude></div>',
link:function(scope, el, attrs, ctrl){
}
}
});
CSS:
.ng-scope{
border:1px solid red;
margin:10px;
}
感謝。我做了一個更簡單的例子[jsfiddle](http://jsfiddle.net/RHLzK/5/)。但是這是一個bug還是它的一個特性? – Jossi
這是子範圍的正常行爲(它們通過父範圍的原型繼承,這意味着您可以從父範圍讀取,但只要您編寫,它就在子範圍內 - 除非您在父範圍workaroud上使用obj ),你可以看看ng-transclude指令的源代碼,複製它,並使用scope:false選項創建自己的代碼。 – Guillaume86
如果我開始使用transclude set作爲'element',問題再次出現。有誰知道爲什麼它現在不起作用。演示:http://plnkr.co/edit/Bv1kFQtzdVzsasHTUrgf?p=preview –