AngularJS指令transclude scope = false? [英] AngularJS directive transclude scope=false?
问题描述
如何防止指令与transclude创建新的范围?
How to prevent directive with transclude to create new scopes?
jsfiddle 我无法绑定任何东西,因为以红色边框显示的新范围。
This jsfiddle I cant bind anything due to the new scopes illustrated with red borders.
HTML:
<div ng-app="components">
<input ng-model="var">
<block>
123
<input ng-model="var">
</block>
</div>
JavaScript:
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:
CSS:
.ng-scope{
border:1px solid red;
margin:10px;
}
推荐答案
(ng-transclude创建一个子范围): https://github.com/angular /angular.js/issues/1056
并在此讨论: https://groups.google.com/forum/#!msg/angular/45jNmQucSCE/hL8x48-JfZIJ
It is actually expected behavior as stated here (ng-transclude create a child scope): https://github.com/angular/angular.js/issues/1056 and discussed here: https://groups.google.com/forum/#!msg/angular/45jNmQucSCE/hL8x48-JfZIJ
您可以解决这通过在范围(obj.var)中的一个对象上设置一个成员就像在这个小提琴中一样: http:/ /jsfiddle.net/rdarder/pnSNj/10/
You can workaround this by setting a member on an object in the scope (obj.var) like in this fiddle: http://jsfiddle.net/rdarder/pnSNj/10/
这篇关于AngularJS指令transclude scope = false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!