Angularjs 递归角度指令

Angularjs 递归角度指令,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我需要在角度指令中使用递归 使用模板遵循代码,不使用递归 它是一个必须递归创建的左菜单 我不知道如何使用递归 我尝试了”,但是只有配置文件图像出现了几次 .directive('leftmenu', ['$compile', function ($compile) { return { retrict: 'E', scope: { menuLeft: '=?' }, template: '<div class="user-panel">' + '<

我需要在角度指令中使用递归

使用模板遵循代码,不使用递归

它是一个必须递归创建的左菜单

我不知道如何使用递归

我尝试了
,但是只有配置文件图像出现了几次

.directive('leftmenu', ['$compile', function ($compile) {
return {
retrict: 'E',
scope: { menuLeft: '=?' },
template: '<div class="user-panel">' +
                '<div class= "pull-left image">' +
                    '<img src="{{ menuLeft.profileImgSrc }}" class="img-circle" alt="{{ menuLeft.profileName  }}" />' + 
                '</div>' + 
                '<div class="pull-left info">' + 
                    '<p>{{ menuLeft.profileName }}</p>' +
                '</div>' +
            '</div>' + 
            '<ul class="sidebar-menu">' +
                '<li class="header">{{ menuLeft.header }}</li>' +
                '<li class="treeview" ng-repeat="itemmenu in menuLeft.itens">' +
                    '<a href="{{ itemmenu.actionLink }}">' +
                        '<i class="{{ itemmenu.visualClass }}"></i> <span>{{ itemmenu.label }}</span>' +
                        '<span class="pull-right-container" ng-show="{{ itemmenu.subItems.length > 0 }}">' +
                            '<i class="fa fa-angle-left pull-right"></i>' +
                        '</span>' +
                    '</a>' +
                    '<ul class="treeview-menu">' +
                        '<li ng-repeat="subItem in itemmenu.subItems">' +
                            '<a ng-if="subItem.openInNewWindow" target="_blank" href="{{ subItem.actionLink }}">' +
                                '<i {{ subItem.class }}></i>{{ subItem.label }}' +
                            '</a>' +
                            '<a ng-if="!subItem.openInNewWindow" target="_self" href="{{ subItem.actionLink }}">' +
                                '<i {{ subItem.class }}></i>{{ subItem.label }}' +
                            '</a>' +
                        '</li>' +
                    '</ul>' +
                '</li>' +
            '</ul>',

compile: function (element) {

},

controller: function ($location, $http, $scope) {

                $scope.menuLeft = {
                    "profileName": "",
                    "profileImgSrc": response.data.msgSaida[0].profileImgSrc,
                    "header": "",
                    "itens": response.data.msgSaida[0].itens,
                    "token": token
                };
            }
        } 
    }
}
.directive('leftmenu',['$compile',function($compile){
返回{
检索:“E”,
作用域:{menuLeft:'=?'},
模板:“”+
'' +
'' + 
'' + 
'' + 
“{{menuLeft.profileName}

”+ '' + '' + “
    ”+ “
  • {{menuLeft.header}
  • ”+ “
  • ”+ '' + “
      ”+ “
    • ”+ '' + '' + “
    • ”+ “
    ”+ “
  • ”+ “
”, 编译:函数(元素){ }, 控制器:函数($location,$http,$scope){ $scope.menuLeft={ “profileName”:“, “profileImgSrc”:response.data.msgSaida[0]。profileImgSrc, “标题”:“, “itens”:response.data.msgSaida[0]。itens, “代币”:代币 }; } } } }

}

聚会晚了一点,如果您仍然希望用递归指令解决这个问题,这取决于您的数据结构是什么样子的,但假设您发布的内容是这样的:

{
    "profileName": "",
    ...
    "items": [{
        "label": "title1",
        "class": "class1",
        "items: [...]
    },{
        "label": "title1",
        "class": "class1",
        "items: [...]
    }]
}
您可以将它们分为两个指令:

  • 一个是包含顶层的父侧面板指令
  • 一个是menuList指令,它将递归地显示子项
//配置文件图像
//配置文件名
//等
其中菜单列表模板类似于:

<ul>
   <li ng-repeat="item in items">
       <span class="{{item.class}}">{{item.label}}</span>
       <menu-list ng-if="item.items" items="item.items"></menu-list>
   </li>
</ul>
  • {{item.label}

因此,在菜单对象的每个实例中(标签、类、项)这些项目将转换为子菜单列表。您可能需要稍微使用它,并添加一些其他功能。

晚会晚了一点,如果您仍希望使用递归指令解决此问题,这取决于数据结构的外观,但假设从您发布的内容来看:

{
    "profileName": "",
    ...
    "items": [{
        "label": "title1",
        "class": "class1",
        "items: [...]
    },{
        "label": "title1",
        "class": "class1",
        "items: [...]
    }]
}
您可以将它们分为两个指令:

  • 一个是包含顶层的父侧面板指令
  • 一个是menuList指令,它将递归地显示子项
//配置文件图像
//配置文件名
//等
其中菜单列表模板类似于:

<ul>
   <li ng-repeat="item in items">
       <span class="{{item.class}}">{{item.label}}</span>
       <menu-list ng-if="item.items" items="item.items"></menu-list>
   </li>
</ul>
  • {{item.label}

因此,在菜单对象(标签、类、项)的每个实例中,项都将转换为子菜单列表。您可能需要稍微使用它并添加一些其他功能。

可能只有我一个人,但我无法理解您的要求。您能添加一些用例和示例线框吗(从Microsoft paint绘制的框也可以)了解它可能只有我一个人,但我无法理解您的要求。您能否添加一些用例和示例线框(从Microsoft paint绘制的框也可以)来了解它