带有单个对象的另一个指令内的指令

时间:2014-04-08 17:12:32

标签: angularjs angularjs-directive angularjs-scope

我有一个看起来像这样的对象:

RecruitingGroups 
{
   GroupName = "stuff",
   GroupTarge = 1,
   Rule =
   {
     Id = 1,
     Match = All,
     Tags = 
     {
       1,
       2,
     }
   }
}

My Angular就像这样:

app.directive('creategroup', function () {
    return {
        restrict: 'E',
        templateUrl: '../../Content/templates/RecruitingGroups/CreateGroup.html',
        link: function (scope, el) {
            scope.groups = data.RecruitingGroups;

            scope.createGroup = function () {
                scope.groups.push({ id: scope.groups.length });
            }
        }
    }
});

app.directive('group', function () {
    return {
        restrict: 'E',
        scope: true,
        templateUrl: '../../Content/templates/RecruitingGroups/Group.html',
        link: function (scope, el) {
            scope.rules = [];
            scope.addRule = function () {
                scope.rules.push({ id: scope.rules.length });
            }
        }
    }
});

app.directive('rule', function () {
    return {
        restrict: 'E',
        scope: true,
        templateUrl: '../../Content/templates/RecruitingGroups/Rule.html',
        link: function (scope, el) {
            scope.tags = [];
            scope.addTag = function () {
                scope.tags.push({ id: scope.tags.length + 1 });
            }
        }
    }
});

我在这里努力争取单词,但是如何使规则对象(它是组对象的子对象)落在指令的正确位置。也许显示预期输出的屏幕截图可以解释更多...... Groups

请注意,图片中有两个组,每个组在每个组中都有一个规则。然后每个规则都有一个标签。这就是我拍摄的输出。

1 个答案:

答案 0 :(得分:1)

我会为规则和组创建范围数组,并根据需要使用ng-repeat重复我的指令标记。

<group ng-repeat="group in groups">
   <rule ng-repeat="rule in group.rules></rule>
</group>

其中group是一个对象数组,其中每个对象(组)都有一个属性规则,它是一个对象数组(规则)