简体   繁体   English

如何在一个指令中包含多个div?

[英]How to include multiple divs in a single directive?

I am trying to include multiple div's in a single directive.But the directive works on only first div and it leaves the other div's inside it. 我试图在一个指令中包含多个div,但该指令仅在第一个div上起作用,并且将另一个div留在其中。

Template code: 模板代码:

<div actor-check-box  ng-repeat="actor in actors">

    <div  create-connections class="actor\{{$index}}" >
            <span><input class="checkbox-actor" type="checkbox" name="actor-checkbox"  id="actor-checkbox\{{$index}}">\{{actor}}</span>
    </div>

    <div ng-repeat="activity in activities">

        <div ng-if="actor == activity.id">

            <div ng-repeat = "impact in activity.text" >
                <div update-connections class="impact\{{$index}}actor\{{actors.indexOf(actor)}}" actor-id="actor\{{actors.indexOf(actor)}}">
                    <span><input class="checkbox-activity" type="checkbox" name="impact-checkbox" id="activity-checkbox\{{$index}}actor\{{actors.indexOf(actor)}}" activity-check-box>\{{impact}}</span>
                </div>

                <div ng-repeat="feature in features">

                    <div ng-if="actor == feature.id && impact == feature.key">
                        <div feature-connection ng-repeat = "feature in feature.text" class="feature\{{$index}}" activity-id="impact\{{activity.text.indexOf(impact)}}actor\{{actors.indexOf(actor)}}" id="">
                                 <span><input class="checkbox" type="checkbox" name="impact-checkbox" id="" >\{{feature}}</span>
                        </div>
                    </div>

                </div>

            </div>

        </div>

    </div>

</div>

Directive Code: 指令代码:

angular.module('mainModule').directive('actorCheckBox', function($interval) {

    return {
        restrict: 'EA',
        /*transclude: true,*/

        link: function (scope, element, attrs, ctrl) {

            scope.$watch('ngModel', function(newValue){
                /*alert(newValue);*/
                console.log(element.find('input[type="checkbox"]'));
                /*console.log(element.find('input[class="checkbox-actor"]'));*/
                console.log(element.find('input[class="checkbox-activity"]'));
                console.log(attrs);

            });
        }
    }
});

Output In console: 输出在控制台中:

[input#actor-checkbox0.checkbox-actor, prevObject: o.fn.init[1], context: div.ng-scope, selector: "input[type="checkbox"]"]
[input#actor-checkbox1.checkbox-actor, prevObject: o.fn.init[1], context: div.ng-scope, selector: "input[type="checkbox"]"]
[input#actor-checkbox2.checkbox-actor, prevObject: o.fn.init[1], context: div.ng-scope, selector: "input[type="checkbox"]"]

So the problem is I have 3 div's and directive is applied on the first div and 2 more div is inside the main div with checkbox. 所以问题是我有3个div和指令应用在第一个div上,另外2个div在主div内有复选框。 When the directive is called console only shows the checkbox element in main div not of other 2 div's. 当指令被调用时,控制台只显示主div中的checkbox元素而不是其他2 div的复选框元素。 Adding the console out from directive as well: 从指令添加控制台:

If I inlude transclude: true it removes all the earlier directives on the element and page goes blank. 如果我包含transclude:true它会删除元素上的所有早期指令,并且页面变为空白。

It's not happening because ng-repeat creates separate scopes for itself. 这是没有发生的,因为ng-repeat为其自身创建了单独的作用域。 You need to include in on the divs as well or modify your directive to not use isolated scopes. 您还需要包含div,或者修改指令以不使用隔离范围。

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

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