Jos*_*ssi 6 html javascript binding angularjs
如何通过transclude来防止指令创建新的范围?
这个jsfiddle我不能绑定任何东西,因为新的范围用红色边框说明.
HTML:
<div ng-app="components">
<input ng-model="var">
<block>
123
<input ng-model="var">
</block>
</div>
Run Code Online (Sandbox Code Playgroud)
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){
}
}
});
Run Code Online (Sandbox Code Playgroud)
CSS:
.ng-scope{
border:1px solid red;
margin:10px;
}
Run Code Online (Sandbox Code Playgroud)
它实际上是预期的行为,如此处所述(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/
| 归档时间: |
|
| 查看次数: |
5926 次 |
| 最近记录: |