简体   繁体   中英

How to use two angular.js directives?

I have defined two directives to connect dom elements on the screen. Both the directives works fine when used as a seperate directives. I am trying to use one directive element in other directive but failed so far. Here is my code.

angular.module('mainModule').directive('createConnections', function($interval) {
    return {
        restrict: 'EA',
        link: function(scope, element, attrs) {

            element.connections({ from:'div.new-div' }).length;
            /*element.connections(attrs).length;*/
            var connections = angular.element('connection, inner');
            $interval(function() { connections.connections('update') }, 10);
        }
    };
});


angular.module('mainModule').directive('updateConnections', function($interval) {
    return {
        restrict: 'EA',

        link: function(scope, element, attrs) {


            element.connections({ from:'div.actor{{index}}' }).length;
            /*element.connections(attrs).length;*/
            var connections = angular.element('connection, inner');
            $interval(function() { connections.connections('update') }, 10);
        }
    };
});

templte code:

 <div create-connections class="actor\{{$index}}" >

        <span><button class=" aui-button dialog-show-button-impact"  style="float: left" title="$i18n.getText('add.activity')"
                      id ="div.actor-\{{$index}}"  ng-click="addActivity(actor)"><span class="aui-icon aui-icon-small aui-iconfont-add">Add</span>
        </button>\{{actor}}</span>
    <br/>
</div>



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



        <div  update-connections ng-if="actor == activity.id" style="margin-left: -45%;">

            <div class="actor\{{actors.indexOf(actor)}}">
            </div>


                <div  ng-repeat = "impact in activity.text" class="impact\{{$index}}" id="">

                    \{{impact}}</div>
            </div>

        </divupdate-connections>

 </div>

I am not able to connect the second level of element to their respective parents element. 在此处输入图片说明

replace createConnection directive

angular.module('mainModule').directive('createConnections', function($interval) {
return {
    restrict: 'EA',
    transclude:true,
    link: function(scope, element, attrs) {

        element.connections({ from:'div.new-div' }).length;
        /*element.connections(attrs).length;*/
        var connections = angular.element('connection, inner');
        $interval(function() { connections.connections('update') }, 10);
    },
    template:'<div ng-transclude></div>
};
});

added an attribute inside update connection name actor-id.

updated the directive to get the attrs actor-id and connected it with the element.

angular.module('mainModule').directive('updateConnections', function($interval) {
    return {
        restrict: 'EA',

        link: function(scope, element, attrs) {

            attrs.$observe('actorId', function(value) {
                console.log('actorId=', value);

               /* value =" '"+ "div." + value + "' ";
                console.log("Connection value:" + value)*/

                element.connections({ from: 'div.'+ value}).length;
                element.connections(attrs).length;
                var connections = angular.element('connection, inner');
                $interval(function() { connections.connections('update') }, 10);
            });


        }
    };
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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