递归地嵌套angularjs指令

时间:2016-03-21 17:36:15

标签: javascript angularjs angularjs-directive

我在我的应用程序中遇到问题,我希望有一个指令,并且在其模板中,它应该有条件地决定是否在其中递归地呈现相同的指令。我试图重新创建它,这个例子也没有用。该指令未呈现且没有错误。不过,我本可以犯一个不同的错误。

https://plnkr.co/edit/PhDvLZyWvyFThg57qZDV?p=preview

的index.html

<!DOCTYPE html>
<html>

  <head>
    <script data-require="angularjs@1.5.0" data-semver="1.5.0" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="app" ng-controller="MainCtrl">
    <ul class="nav navbar-nav">
      <li ng-repeat="menu in menus">
        <button class="btn btn-default dropdown-toggle" type="button">
          <span ng-bind="menu.Text"></span>
          <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" role="menu">
          <li menu-entry menus="menu.SubMenus"></li>
        </ul>
      </li>
    </ul>
  </body>

</html>

菜单-entry.html

<li ng-repeat="menu in menus">
    <a ng-if="menu.Submenus.length===0" ng-bind="menu.Text"></a>
    <button ng-if="menu.Submenus.length>0" type="button" ng-bind="menu.Text">
        <span class="caret caret-right"></span>
    </button>
    <ul ng-if="menu.Submenus.length>0" class="dropdown-menu" role="menu">
        <li menu-entry menus="menu.SubMenus"></li>
    </ul>
</li>

的script.js

var app = angular.module('app', []);

app.controller('MainCtrl', function($scope) {
  $scope.menus = [
    { Text: '1', SubMenus: [
      { Text: '1.1', SubMenus: [{Text:'1.1.1',SubMenus:[]},{Text:'1.1.2',SubMenus:[]},{Text:'1.1.3',SubMenus:[]}]},
      { Text: '1.2', SubMenus: [{Text:'1.2.1',SubMenus:[]},{Text:'1.2.2',SubMenus:[]},{Text:'1.2.3',SubMenus:[]}]},
      { Text: '1.3', SubMenus: [{Text:'1.3.1',SubMenus:[]},{Text:'1.3.2',SubMenus:[]},{Text:'1.3.3',SubMenus:[]}]}
      ]},
    { Text: '2', SubMenus: [
    { Text: '2.1', SubMenus: [{Text:'2.1.1',SubMenus:[]},{Text:'2.1.2',SubMenus:[]},{Text:'2.1.3',SubMenus:[]}]},
    { Text: '2.2', SubMenus: [{Text:'2.2.1',SubMenus:[]},{Text:'2.2.2',SubMenus:[]},{Text:'2.2.3',SubMenus:[]}]},
    { Text: '2.3', SubMenus: [{Text:'2.3.1',SubMenus:[]},{Text:'2.3.2',SubMenus:[]},{Text:'2.3.3',SubMenus:[]}]}
    ]},
    ];
});

app.directive('menuEntry', function() {
        var cFn = ['$scope', function ($scope) {

        }];

        var lFn = function (scope, element, attr, ctrl, transclude) {
        };

        return {
            restrict: 'A',
            replace: true,
            templateUrl: 'menu-entry.html',
            controller: cFn,
            link: lFn,
            scope: {
                menus: '='
            }
        };
});

2 个答案:

答案 0 :(得分:2)

您当前的指令实现基本上存在3个问题。

  • 问题是您对li元素的指示replace: true正在添加一个又有ul的模板。因此,生成的html格式不正确,因为您无法再次在ulul将指令replace: true更改为replace: false **或只需删除该选项。**
  • 您的指令模板menu.Submenus应该是menu.SubMenus

<强>模板

<li ng-repeat="menu in menus">
  {{menu.Text}}
    <a ng-if="menu.SubMenus.length == 0" ng-bind="menu.Text"></a>
    <button ng-if="menu.Submenus.length>0" type="button" ng-bind="menu.Text">
        <span class="caret caret-right"></span>
    </button>
    <ul ng-if="menu.SubMenus.length>0" class="dropdown-menu" role="menu">
        <li menu-entry menus="menu.SubMenus"></li>
    </ul>
</li>

Demo Plunkr

答案 1 :(得分:1)

问题是拼写错误: 子菜单 - &gt;子菜单

您使用SubMenus在script.js对象中定义,但您在模板中引用了Submenus。