如何使用两个angular.js指令?

时间:2016-03-28 10:01:34

标签: javascript jquery angularjs

我已经定义了两个指令来连接屏幕上的dom元素。当用作单独的指令时,这两个指令都可以正常工作。我试图在其他指令中使用一个指令元素但到目前为止失败了。这是我的代码。

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

临时代码:

 <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>

我无法将第二级元素连接到各自的父元素。 enter image description here

2 个答案:

答案 0 :(得分:0)

替换createConnection指令

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

答案 1 :(得分:0)

在更新连接名称actor-id中添加了一个属性。

更新了指令以获取attrs actor-id并将其与元素连接。

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


        }
    };
});