[英]AngularJS directive transclude scope=false?
如何通过transclude来防止指令创建新的范围?
这个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;
}
它实际上是预期的行为,如此处所述(ng-transclude创建子范围): https : //github.com/angular/angular.js/issues/1056并在此处讨论: https : //groups.google.com/forum /#!味精/角/ 45jNmQucSCE / hL8x48-JfZIJ
您可以通过在范围(obj.var)中的对象上设置成员来解决此问题,就像在这个小提琴中一样: http : //jsfiddle.net/rdarder/pnSNj/10/
虽然Angular团队不推荐这样做,但另一种解决方法是在transcluded部分中显式调用$ parent范围:
<div ng-app="components">
<input ng-model="var">
<block>
123
<input ng-model="$parent.var">
</block>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.