0

I created a custom directive.

angular.module('menu',[])
    .directive('Menu',function(){
        return{
            restrict:'E',
            replace:'true',
            scope:{
                menuList:'=',
                id:'@',
                parentId:'@'
            },
            controller:function($scope){       
            },
            template: `
                <div ng-repeat="menuItem in menuList">
                    <a>{{menuItem.longDescription}}</a>
                    <div ng-if="menuItem.child.size() > 0 ">
                      <menu menu-list="menuItem.child" 
                            id="menuItem.optionGroupId"
                            parent-id="menuItem.parentOptionGroupId">
                      </menu>
                    </div>
                </div>
            `                
        }
    })

Menu items can have child list and if child list exist it should make menu recursively, but recursion is not happening.

3
  • can you try to change the directive name from "Menu" to "some other name" ? Hope it will work. Commented Oct 14, 2019 at 9:28
  • already tried. nothing happened Commented Oct 14, 2019 at 9:34
  • This might help you. check out stackoverflow.com/questions/14430655/…. Commented Oct 14, 2019 at 10:02

1 Answer 1

1

(function() {
  angular.module("menuTest", [])
    .controller('testCtrl', function($scope) {
      $scope.menu = [{
          id: 1,
          children: [{
            id: 3
          }]
        },
        {
          id: 2,
          children: [{
              id: 4
            },
            {
              id: 5
            }
          ]
        }
      ];
    })
    .directive("menu", function() {
      return {
        restrict: "E",
        replace: true,
        scope: {
          menuList: '='
        },
        template: '<div ng-repeat="menuItem in menuList">{{menuItem.id}}<div ng-if="menuItem.children && menuItem.children.length>0" ><menu menu-list="menuItem.children"></menu></div><div>'
      }
    })
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<div ng-app="menuTest" ng-controller="testCtrl">
  <menu menu-list="menu"></menu>
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

Can you please explain what am I doing wrong? I see no difference between your code and my code. only difference is that I am using Angular 1.3x
size() is jQuery function. Did you add jQuery?
oh Thanks!! after using length is working fine now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.