【问题标题】:Transclude on ng-repeat在 ng-repeat 上嵌入
【发布时间】:2015-01-18 18:25:50
【问题描述】:

我试图在指令中重复一个指令,这样我就可以在每个指令上都有一个模板,但问题是我只能在使用 ng-transclude

时显示第一个条目

这是我目前所做的

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

对于控制器

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

我只得到鼠标

我准备了一个小提琴来看看它的实际效果CLICK HERE

请帮忙。

提前谢谢你

【问题讨论】:

    标签: javascript angularjs angularjs-directive angularjs-ng-repeat angularjs-ng-transclude


    【解决方案1】:

    你弄乱了你的指令&lt;test-data&gt; 标签。您还没有关闭test-data。你给 test-data 元素加上了自我结束标签,比如 &lt;test-data /&gt;

    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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多