在 ng-repeat 上嵌入
Transclude on ng-repeat
我试图在指令中重复一个指令,这样我就可以在每个指令上都有一个模板,但问题是我只能在使用 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>'
}
});
我只得到鼠标
我准备了一个 fiddle 来看看它的实际效果 CLICK HERE
请帮忙。
提前致谢
您弄乱了指令 <test-data>
标记。您还没有关闭 test-data
。您给 test-data
元素添加了自闭标签,例如 <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>
我试图在指令中重复一个指令,这样我就可以在每个指令上都有一个模板,但问题是我只能在使用 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>'
}
});
我只得到鼠标
我准备了一个 fiddle 来看看它的实际效果 CLICK HERE
请帮忙。
提前致谢
您弄乱了指令 <test-data>
标记。您还没有关闭 test-data
。您给 test-data
元素添加了自闭标签,例如 <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>