【问题标题】:Angular append an element to the child of current element inside of a directiveAngular将一个元素附加到指令内的当前元素的子元素
【发布时间】:2014-04-03 01:34:49
【问题描述】:

我正在尝试将元素附加到指令元素内的子元素。所以我有这个代码:

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

请注意链接中我有一个函数()。该函数具有传递给它的范围和元素。我认为 'el' 会是独一无二的,就像做一个 jQuery addRule($this)。但似乎情况并非如此,因为如果这些&lt;group&gt; div 中有几个在页面上,则代码$(el).find('.getGroups').append(rule) 将附加到页面上该元素的最后一个实例。不要像我想象的那样去$this

我的问题是如何将此&lt;rule&gt; 元素附加到this 指令。页面上没有另一个。

一些示例 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>

换句话说,当我单击addRule() 时,我希望它将&lt;rule&gt; 元素附加到您当前单击addRule() 按钮的子id="rule"

【问题讨论】:

    标签: javascript jquery angularjs angularjs-directive


    【解决方案1】:

    除了 Patrick 关于范围的见解之外,您还可以通过让模型驱动 DOM 来避免使用 jQuery。这是一个简单的演示: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 模板:

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

    【讨论】:

    • 但这不会让规则在页面加载时自动生成吗?我想在用户单击按钮时添加整个规则元素。
    • 不,这只会在 scope.rules 中有规则时创建规则元素。您可以使用规则或什么都不初始化该数组。由你决定。
    • @allencoded,在我的回答中试试 plunker。您可以看到在单击按钮之前没有创建任何规则。
    • 我做到了,而且效果很好我什至添加到它上面来做我真正需要的embed.plnkr.co/QKCd1LWhVjA47loohAom/preview
    【解决方案2】:

    您的问题是您没有创建新范围。因此,每次添加 group 指令时,实际上都是在替换“旧”的 addRule 函数。

    尝试将 scope: true 添加到您的指令定义中。示例:

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

    这将在原型上继承父作用域,然后您将能够为每个作用域定义函数 addRule。在这种情况下,元素 (el) 应该是您所期望的。

    按照 dave 的建议,将 id 替换为 class 也可能会很好,因为 id 在文档中是唯一的。

    【讨论】:

    • 另外,#rule 对于页面应该是唯一的。由于您正在重复元素,因此 id 不是正确的选择,您应该使用类。
    猜你喜欢
    • 1970-01-01
    • 2021-10-26
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 2018-04-26
    • 2018-07-04
    相关资源
    最近更新 更多