私はいくつかの角度指令を持っています。
タイプに応じてng-repeatのアコーディオン・ディレクティブにそれらを含める必要があります。
私はそれを再利用できるようにしたいので、レンダリングされている指示の種類をアコーデオンに知らせたくありません。
そのため、私はアコーディオンの内部でng-switch命令を使用したくありません。 非常に簡単なデモです。実際には、これらのディレクティブは、コンパイルするための独自の属性を持ちます。ng-repeatに異なる角度指令を含める
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>
ありがとう!
elementAccordionディレクティブに属性として 'elements'を渡す必要がありますか? – hansmaad
@hansmaadはい、例を単純化するために省略しました。 – airnan