AngularJs,从嵌套ng-repeat内的指令访问父作用域

斯特凡诺·卡斯特里奥塔(Stefano Castriotta)

这是一个菜鸟问题。我正在寻找访问嵌套ng-repeat中的指令内的父作用域的正确方法。这正是我的意思:

<div ng-app="myApp" ng-controller="myCtrl">
    <div ng-repeat="section in sections">
        {{section.Name}}
        <div ng-repeat="item in section.Items" ng-init="parent = section">
            <span class="menuItem">{{item}}</span>
        </div>
    </div>
</div>

和指令:

myApp.directive('menuItem', function () {
    return {
        restrict: 'C',
        link: function (scope, element, attrs) {
            console.log(scope.$parent.section.SectionId);
        }
    }
});

该指令附加到内部ng-repeat中的项目,我需要访问父对象的属性。问题是我无法直接访问父属性(具有scope。$ parent),因为ng-repeat创建了一个新的范围,并且我必须附加在ng-repeat中设置的对象的名称(在这种情况下为scope 。$母公司部分):

<div ng-repeat="section in sections">

console.log(scope.$parent.section.SectionId);

JsFiddle:http : //jsfiddle.net/7Lra7Loy/2/

由于我希望该指令具有通用性,因此可以在其他ng-repeat块中使用它,而不必在ng-repeat中强制使用相同的名称,因此,我发现的唯一方法是使用ng-init,这将在所有ng-repeat块中都相同(ng-init =“ parent = section”):

<div ng-repeat="section in sections">
    {{section.Name}}
    <div ng-repeat="item in section.Items" ng-init="parent = section">
        <span class="menuItem">{{item}}</span>
    </div>
</div>

myApp.directive('menuItem', function () {
    return {
        restrict: 'C',
        link: function (scope, element, attrs) {
            console.log(scope.parent.SectionId);
        }
    }
});

JsFiddle:http : //jsfiddle.net/7Lra7Loy/1/

有没有更好的方法来处理这种情况?还是我只是想念一些东西?我搜索了一下,但是找不到任何真正有用的东西。

阿列克谢·阿夫捷耶夫(Alexey Avdeyev)

模板:

<div ng-app="myApp" ng-controller="myCtrl">
    <div ng-repeat="section in sections">
        {{section.Name}}
        <div ng-repeat="item in section.Items">
            <span class="menuItem" section="{{section}}">{{item}}</span>
        </div>
    </div>
</div>

和指令:

myApp.directive('menuItem', function () {
    return {
        restrict: 'C',
        scope: {
            section: '@' // text-binding
            //section: '&' //one-way binding
            //section: '=' //two-way binding
        },
        link: function ($scope, element, attrs) {
            console.log($scope.section);
        }
    }
});

JsFiddle:https ://jsfiddle.net/nrkmn/26zhqbjg/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

嵌套指令后,如何访问特定的父作用域?

Angularjs - ng-repeat 和嵌套作用域

AngularJS:指令更新ng-repeat内的父范围

AngularJS,嵌套JSON和ng-repeat

如何访问嵌套指令中的作用域?

如何在AngularJS中的自定义指令*中使用自己的作用域*访问父作用域?

ANgularjs:ng-repeat和嵌套的自定义指令

AngularJs 1.6 - 在指令“链接”函数中无法访问父作用域

在ng-repeat指令中进行角度选择:如何访问子作用域?

AngularJS如何从指令访问Controller作用域

AngularJS-如何访问嵌套的ng-repeat中的数组

AngularJS嵌套ng-repeat我如何从父控制器中引用子作用域?

具有隔离作用域、ng-repeat 和 controllerAs 的 AngularJS 指令

AngularJs指令:从模板中的父作用域调用方法

为什么ng-repeat指令创建子作用域?

AngularJS:在嵌套的ng-repeat中迭代时计数

AngularJS 中带有 $index 的嵌套循环(ng-repeat)

带有ng-repeat的AngularJS中的嵌套注释

在AngularJS中对嵌套ng-repeat的元素进行排序

Angularjs嵌套ng-repeat有条件的原因

AngularJS:嵌套循环中$ index的ng-repeat跟踪

嵌套 ng-repeat 创建更多标签 angularjs

带有嵌套ng-repeat的AngularJS Filter数组

嵌套的ng-repeat不会出现angularjs

未选中嵌套ng-repeat中的Angularjs单选按钮

嵌套的json中的angularjs ng-repeat下拉菜单

AngularJS嵌套数组中的ng-repeat

angularjs从父级更改ng-repeat嵌套值

在angularjs中嵌套ng-mouseover不起作用