[英]AngularJS override scope of transclude scope
Initialize of the dir call: 初始化dir调用:
<div ng-app="myApp">
<dir arr="[{name: 'Dennis'}, {name: 'Bob'}]">
<h3>Working as not wanted: {{$parent.obj.name}}</h3>
<h3>Not working as wanted: {{obj.name}}</h3>
</dir>
</div>
And the directive is self: 指令是self:
angular.module("myApp",[]).directive('dir', function(){
return {
restrict: 'E',
transclude: true,
scope: {
arr: '='
},
template: '<span ng-repeat="obj in arr"><ng-transclude></ng-transclude></span>',
link: function(scope, iElement, iAttrs, controller, transcludeFn) {
//transcludeFn(scope);
}
}
});
From docs transcludeFn
with first argument should replace the scope of the transclude isolated scope, but it doesn't. 从文档中,带第一个参数的
transcludeFn
应该替换独立的transclude范围的范围,但不是。 Here is the codepen working sample to test/work with. 这是要测试/使用的Codepen工作示例。
ng-transclude will leave the content as it is whatever inside the directive element and those elements inside directive still look for a parent scope only. ng-transclude会将内容保留在指令元素内部,而指令内部的那些元素仍仅查找父作用域。
<div ng-app="myApp">
<dir arr="[{name: 'Dennis'}, {name: 'Bob'}]">
<h3>Working as not wanted: {{$parent.obj.name}}</h3>
</dir>
</div>
angular.module("myApp",[]).directive('dir', function(){
return {
restrict: 'E',
transclude: true,
scope: {
arr: '='
}, template: '<span ng-repeat="obj in arr"><ng-transclude></ng-transclude> <h3>Not working as wanted: {{obj.name}}</h3></span>',
link: function(scope, iElement, iAttrs, controller, transcludeFn) {
//transcludeFn(scope);
}
}
});
ng-transclude
will take the scope
of the directive
in which it is present ng-transclude
将采用其所在directive
的scope
Please check this directive code: 请检查以下指令代码:
angular.module("myApp",[]).directive('dir', function(){
return {
restrict: 'E',
transclude: true,
scope: {
arr: '='
}, template: '<span ng-repeat="obj in arr"><ng-transclude></ng-transclude></span>',
link: function(scope, el, attrs, ctrl, transclude) {
scope.arr = [{name: 'Dennis'}, {name: 'Bob'}];
transclude(scope, function(clone, scope) {
// this takes the parent scope
});
}
}
});
Html: HTML:
<div ng-app="myApp">
<dir arr="arr">
<h3>Working as not wanted: {{$parent.obj.name}}</h3>
<h3>Not working as wanted: {{arr | json}}</h3>
</dir>
</div>
After huge research, I figure out that, until now there is no way to modify the scope of translucde. 经过大量研究,我发现,到目前为止,尚无办法修改半透明的范围。 So I moved out the repeat, which broke a bit the logic of this quesiton and as temporary solution I made this one.
因此,我搬出了重复的书,这有点打破了这个问题的逻辑,而作为临时解决方案,我做了这个。 html:
HTML:
<div ng-app="myApp" ng-controller="app">
<dir obj="obj" ng-repeat="obj in arr">
<h3>Looking to get: {{obj.name}}</h3>
</dir>
</div>
and javascript: 和javascript:
ular.module("myApp", []).controller('app', function($scope){
$scope.arr = [{name: 'Dennis'}, {name: 'Bob'}];
}).directive('wsort', function(){
return {
restrict: 'E',
transclude: true,
scope: {
obj: '='
}, template: '<ng-transclude></ng-transclude>'
}
});
Where we specify repeat outside of the directive and use the obj within as we need. 我们在指令外指定重复的位置,并根据需要在其中使用obj。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.