【发布时间】:2026-02-23 02:30:02
【问题描述】:
我有几个角度指令。
我想根据类型将它们包含在带有 ng-repeat 的手风琴指令中。
我希望它是可重用的,所以我不希望手风琴知道它正在渲染的指令类型。
这就是我不想在手风琴内部使用 ng-switch 指令的原因。
这是一个非常简化的演示。实际上,这些指令将具有自己的属性,以便 Angular 进行编译。
var testApp = angular.module('testApp', []);
(function() {
function Element1() {
return {
template: '<span>hello</span>',
restrict: 'E',
replace: true
}
}
testApp.directive('elementOne', Element1);
}());
(function() {
function Element2() {
return {
template: '<span>world</span>',
restrict: 'E',
replace: true
}
}
testApp.directive('elementTwo', Element2);
}());
(function() {
function Accordion() {
return {
template: '<ul><li ng-repeat="element in elements"><button>Toggle</button> Here should be the directive</li></ul>',
restrict: 'E',
replace: true,
controller: function($scope) {
$scope.elements = [{
type: 1
}, {
type: 2
}];
}
}
}
testApp.directive('elementAccordion', Accordion);
}());
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="testApp">
<element-accordion></element-accordion>
</div>
谢谢!
【问题讨论】:
-
是否应该将
elements作为属性传递给 elementAccordion 指令? -
@hansmaad 是的,我跳过它以简化示例。
标签: angularjs angularjs-directive angularjs-ng-repeat