简体   繁体   English

Angular将一个元素附加到指令内当前元素的子元素

[英]Angular append an element to the child of current element inside of a directive

I am trying to append an element to a child element inside of a directives element. 我试图将一个元素附加到指令元素内的子元素。 So I have this code: 所以我有这个代码:

directives.group = function ($compile) {
    return {
        restrict: 'E',
        templateUrl: '../../Content/templates/RecruitingGroups/Group.html',
        link: function (scope, el) {
            scope.addRule = function () {
                var rule = $compile('<rule></rule>')(scope);
                $(el).find('#rule').append(rule);
            }
        }
    }
}

Notice in the link I have a function(). 注意在链接中我有一个函数()。 The function has scope and element passed into it. 该函数具有传递给它的范围和元素。 I thought 'el' would be unique and would be like doing a jQuery addRule($this) . 我认为'el'会是独一无二的,就像做一个jQuery addRule($this) But it appears this is not the case because if several of these <group> divs are on the page the code $(el).find('.getGroups').append(rule) will append to the last instance of that element on the page. 但看起来并非如此,因为如果这些<group> div中有几个在页面上,代码$(el).find('.getGroups').append(rule)将附加到该元素的最后一个实例上这一页。 Not to $this like I imagined it would work. 不要$this就像我想象它会工作。

My problem is how do I append this <rule> element to this directive. 我的问题是如何将此<rule>元素附加this指令。 Not another one an the page. 不是另一个页面。

Some Example HTML 一些示例HTML

<div>
    <group>
        <div ng-click="addRule()">
            <div id="rule">
                <rule></rule>
                <rule></rule>
            </div>
        </div>
    </group>
    <group>
        <div ng-click="addRule()">
            <div id="rule">
                <rule></rule>
            </div>
        </div>
    </group>
</div>

So in other words When I click addRule() I want it to append the <rule> element to the child id="rule" where you currently clicked the addRule() button. 换句话说,当我点击addRule()我希望它将<rule>元素附加到当前单击addRule()按钮的child id="rule"

Your problem is that you are not creating a new scope. 您的问题是您没有创建新范围。 So everytime you add the group directive, you are essentially replacing the "old" addRule function. 因此,每次添加group指令时,您基本上都会替换“旧”addRule函数。

Try adding scope: true to your directive definition. 尝试将scope:true添加到指令定义中。 Example: 例:

directives.group = function ($compile) {
    return {
        restrict: 'E',
        scope: true,
        templateUrl: '../../Content/templates/RecruitingGroups/Group.html',
        link: function (scope, el) {
            scope.addRule = function () {
                var rule = $compile('<rule></rule>')(scope);
                $(el).find('#rule').append(rule);
            }
        }
    }
}

This will prototypically inherit the parent scope and then you will be able to define function addRule for each. 这将原型继承父作用域,然后您就可以为每个作用域定义函数addRule。 In this case, the element (el) should be what you expect. 在这种情况下,元素(el)应该是您所期望的。

It would also probably be good to replace id with class as dave suggests since an id is intended to be unique within a document. 如dave所建议的那样,将id替换为类也可能是好的,因为id在文档中是唯一的。

In addition to Patrick's insight about scope, you may be able to avoid using jQuery by letting your model drive the DOM. 除了Patrick对范围的深入了解之外,您可以通过让模型驱动DOM来避免使用jQuery。 Here is a simple demo: http://plnkr.co/BD7cKTfR4iWhM5pHPoD4 这是一个简单的演示: http//plnkr.co/BD7cKTfR4iWhM5pHPoD4

app.directive('group', function () {
    return {
        restrict: 'E',
        scope: true,
        templateUrl: '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: 'rule.html'
  }
});

group.html template: group.html模板:

<div>
    <input type="button" value="add a rule" ng-click="addRule()" />
    <div>
        <rule ng-repeat="rule in rules"></rule>
    </div>
</div>

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

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