Search code examples
javascriptangularjsangularjs-directiveangularjs-ng-repeatangularjs-ng-transclude

Transclude on ng-repeat


im trying to repeat a directive inside the directive so i can have a template on each of directive but the problem is i can only show the first entry when using ng-transclude

Here is what i've done so far

<div ng-app="TestApp">
    <div ng-controller="TestCtrl">
        <test-collector>
            <test-data xx-value="Mouse" xx-href="https://fb.com" />
            <test-data xx-value="Keyboard" xx-href="https://goo.gl" />
        </test-collector>        
    </div>
</div>

and for the controller

var app = angular.module('TestApp', []);

app.controller('TestCtrl', ['$scope', function($scope){

}]);

app.directive("testCollector", function () {
    return {
        restrict: "E",
        scope: {},
        transclude: true,  
        replace: true,
        controller: function($scope) {

        },
        template: '<div>' +
                        '<div ng-transclude></div>' +
                   '</div>'
    }
});

app.directive("testData", function(){
    return {
        restrict: "E",
        require: '^testCollector',
        transclude: true,
        replace: true,
        scope: {
            xxValue: '@',
            xxHref: "@"
        },
        template: '<a href="{{xxHref}}">{{xxValue}}</a>'
    }
});

i only get Mouse

i prepared a fiddle to see it in action CLICK HERE

any help please.

Thank you in advance


Solution

  • You're messing with your directive <test-data> tag. You haven't close the test-data. You gave self closing tag to test-data element like as <test-data />.

    HTML

    <div ng-app="TestApp">
        <div ng-controller="TestCtrl">
            <test-collector>
                <test-data xx-value="Mouse" xx-href="https://fb.com"></test-data>
                <test-data xx-value="Keyboard" xx-href="https://goo.gl"></test-data>
            </test-collector>        
        </div>
    </div>
    

    Working Fiddle