我试图创建一个可以将两个东西转换成不同位置的指令,其中一个是ng-repeat
,但是在我的编译函数中使用$transclude
会在返回{{1}时中断但是,如果我在undefined is not a function
中执行此功能,但pre-compile
没有内容。
我的代码看起来像这样
ng-repeat
.directive('sidebarItem', ['_', function(_) {
return {
replace: true,
transclude: true,
scope: {
title: '@',
state: '@',
activeState: '=',
dropdown: '='
},
compile: function(ele, attrs, transclude) {
transclude(ele) // This returns error
return {
pre: function(scope, ele,attrs, ctrl, trns) {
trns(scope) // This has my transcluded content but the ng-repeat is empty
}
}
}
}
}
trns(scope.$parent, function(tEle, tScope) {
var repeat = tEle[1].querySelector('[repeat]'),
newEle = angular.element('<li ng-repeat="item in studentDropdown"></li>'),
dropdown = ele[0].querySelector('.dropdown')
tScope.studentDropdown // Can access here
newEle.append(repeat.innerHTML)
angular.injector(['ng']).get('$compile')(newEle) // returns ng-repeat comment but no items
angular.element(dropdown).append(newEle)
})
li.sidebar-item(ng-class='{active: active, dropdown: dropdown, "dropdown-open": isDropdownOpen}')
.sidebar-content
a.sidebar-item-link(ng-if='!dropdown' ui-sref='{{state}}')
a.sidebar-item-link(ng-if='dropdown' ng-click='toggleDropdown()')
.icon(ng-transclude)
.title {{ title }}
.down-arrow(ng-if='dropdown')
ul.dropdown(ng-if='dropdown')
sidebar-item
div(has-dropdown)
div(icon)
//icon stuff
div(dropdown)
li(ng-repeat='item in dropdown')
| {{ item }}