简体   繁体   English

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

[英]AngularJs, accessing parent scope from directive inside a nested ng-repeat

That's a noob question. 这是一个菜鸟问题。 I'm looking for the correct way to access the parent scope inside a directive in a nested ng-repeat. 我正在寻找访问嵌套ng-repeat中的指令内的父作用域的正确方法。 This is exactly what i mean: 这正是我的意思:

<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>

And the directive: 和指令:

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

The directive is attached to an item in the inner ng-repeat, and i need to access a property of the parent object. 该指令附加到内部ng-repeat中的项目,我需要访问父对象的属性。 The problem is that i cannot access directly to the parent properties (with scope.$parent), because ng-repeat creates a new scope, and i must append the name of the object i set in the ng-repeat (in this case scope.$parent. section .): 问题是我无法直接访问父属性(具有scope。$ parent),因为ng-repeat创建了一个新的范围,并且我必须附加在ng-repeat中设置的对象的名称(在这种情况下为scope 。$ parent。 部分 。):

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

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

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

As i want the directive to be generic, so it can be used inside other ng-repeat blocks, without being forced to use the same names in the ng-repeat, the only way i found is to use an ng-init, that would be the same in all ng-repeat blocks (ng-init="parent = section"): 由于我希望该指令具有通用性,因此可以在其他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/ JsFiddle: http : //jsfiddle.net/7Lra7Loy/1/

Is there a better way to handle this situation? 有没有更好的方法来处理这种情况? Or am i just missing something? 还是我只是想念一些东西? I searched a bit, but i couldn't find anything really useful. 我搜索了一下,但是找不到任何真正有用的东西。

Template: 模板:

<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>

And directive: 和指令:

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/ JsFiddle: https ://jsfiddle.net/nrkmn/26zhqbjg/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM