Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS覆盖转移范围的范围_Angularjs_Angularjs Directive - Fatal编程技术网

AngularJS覆盖转移范围的范围

AngularJS覆盖转移范围的范围,angularjs,angularjs-directive,Angularjs,Angularjs Directive,初始化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>

初始化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>
指令是自我:

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应该替换transclude隔离范围的范围,但它不是

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将采用其所在指令的范围

请检查此指令代码:

Html:


经过大量的研究,我发现,到目前为止,还没有办法修改CDE的范围。所以我取消了重复,这打破了这个问题的逻辑,作为临时解决方案,我做了这个。 html:

和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>'
    }
});

我们指定在指令外重复,并根据需要在指令内使用obj。

不明白您到底需要什么?你到底需要什么?我需要ng transclude的作用域与父作用域相同。你做了一件很棒的事情,你在transclude中有自定义作用域,这就是我想要的。但实际上需要的是,由指令创建的作用域被transclude使用,因此我们可以在transclude中使用obj。如果您看到obj没有按需要工作:{{arr | json},它本身就是在使用指令作用域。关键是要保持不按需要工作:{{obj.name}在html中。我添加//transcludeFnscope的原因;是以某种方式提供应禁止使用的作用域。以不需要的方式工作:{{$parent.obj.name}}这一行不应在html中提供不需要的名称:{{obj.name}。我添加//transcludeFnscope的原因;这一行应该给出名称我为您添加了一个答案,这样,您就可以直接在这里检查,而不是到处去那个链接。现在检查我是否将范围从transclude fn更改,它将指令范围更改为scope.arr=[{name:'Dennis'},{name:'Bob'}];这是一个新数组,不是我们从html发送的数组。即使是我在指令范围内分配的新数组。但是not transclude scope\看起来不错,但是{{obj.name}}仍然不起作用
<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>
<div ng-app="myApp" ng-controller="app">
  <dir obj="obj" ng-repeat="obj in arr">
    <h3>Looking to get: {{obj.name}}</h3>
  </dir>
</div>
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>'
    }
});