1

I am new to angular js .I need to create a dynamic menu and hyperlink using angular js. I have menu name and hyperlink coming from json and i need to display. Currently i have tried with static menus which is working.

My menu structure is like

Home

Services

        -ser1
        -ser2
        -ser3

About

    -abt1

Contact

All the menu values and hyperlink comes from json file.

This is my json

 [
    {
        "id": 100,
        "product": 0,
        "childs": [
            {
                "id": 200,
                "description": {
                    "id": 0,
                    "name": "Home",
                    "url": "home"
                }
            }
        ]
    },
    {
        "id": 100,
        "description": {
            "id": 0,
            "name": "services",
            "url": "services"
        },
        "parent": null,
        "childs": [
            {
                "id": 200,
                "description": {
                    "id": 0,
                    "name": "Ser1",
                    "url": "Ser1"
                },
                "productCount": 0,
                "childs": [
                    {
                        "id": 250,
                        "description": {
                            "id": 0,
                            "name": "ser2",
                            "url": "Ser2"
                        },
                        "childs": []
                    },
                    {
                        "id": 251,
                        "description": {
                            "id": 0,
                            "name": "ser3",
                            "url": "ser3"
                        },
                        "productCount": 0,
                        "childs": []
                    }
                ]
            }
        ]
    },
    {
        "id": 201,
        "description": {
            "id": 0,
            "name": "About",
            "url": "about"
        },
        "productCount": 0,
        "childs": [
            {
                "id": 203,
                "description": {
                    "id": 0,
                    "name": "abt1",
                    "url": "underground"
                },
                "productCount": 0,
                "childs": []
            }
        ]
    },
    {
        "id": 202,
        "description": {
            "id": 0,
            "name": "Contact",
            "url": "con"
        },
        "productCount": 0,
        "childs": []
    }
]

And this is my HTML

<li class="prod-dropdown" ng-repeat="menu in menus" ng-class="{proddropdown: menu.menus}">
                    <a ng-href="#/{{menu.action}}" ng-class="{'dropdown-toggle': menu.menus}"
                       data-toggle="dropdown">{{menu.menus.desc['name']}} </a>
                    <ul ng-if="menu.menus" class="dropdown-menu">
                        <li ng-repeat="submenu in menu.menus">
                            <a ng-href="#/{{submenu.action}}">{{submenu.desc}}</a>
                        </li>
                    </ul>
                </li>
6
  • 1
    What are you trying to do? You do not have even a single matching property defined in json and html Commented Jun 20, 2015 at 18:17
  • The values will come from php backend they are creating json dynamically.This is for ecommerce website Commented Jun 20, 2015 at 18:20
  • I am not talking about values but the properties. For instance, in json, I can not find anything like action Commented Jun 20, 2015 at 18:21
  • i need to create a menu like home,services,about,contact And inside services menu there will be three submenus ser1,ser2,ser3 And inside about menu there will be submenu abt1.And all the menu values will come from json Commented Jun 20, 2015 at 18:23
  • Try transforming your JSON into an array matching the structure of your menu object (using JS or PHP) and add it to your controller's $scope. If you have trouble after that point, update your question accordingly. You will likely run into issues with nested child menus; see stackoverflow.com/q/15661289/3149036 when you need it. Commented Jun 20, 2015 at 18:29

4 Answers 4

1

You should use ng-include attribute - http://benfoster.io/blog/angularjs-recursive-templates

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

Comments

1

2 level submenu

<li class="prod-dropdown" ng-repeat="menu in menus">
            <a ng-href="#/{{menu.description['url']}}" ng-class="{'dropdown-toggle': menu.menus}" class="dropdown-toggle" data-toggle="dropdown">
                {{menu.description['name'] | uppercase}} <span ng-if="menu.childs" class="caret"></span>
            </a>
            <ul ng-if="menu.childs" class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
                <li class="dropdown-submenu" ng-repeat="submenu in menu.childs">
                    <a tabindex="-1" ng-href="#/products/{{submenu.description['url']}}">{{submenu
                        .description['name']}}</a>
                    <ul class="dropdown-menu" ng-if="submenu.childs">
                        <li class="dropdown-submenu" ng-repeat="subofsub in submenu.childs">
                            <a ng-href="#/products/{{subofsub.description['url']}}">{{subofsub.description['name']}}</a>
                        </li>
                    </ul>
                </li>
            </ul>
    </li>

Comments

0

Here is an example of "Dynamic multi level Menu"

HTML

    <ul class="sidebar-menu" ng-repeat="m in modulos">
      <li class="treeview" ng-repeat="(itemIndex, item) in modulos">
        <a ng-click="showSubmenu(itemIndex)">
          <i class="fa fa-table"></i> <span>{{item.module}}</span>
        </a>

        <ul class="sub-nav" ng-show="isShowing(itemIndex)">
          <li ng-repeat="sub_element in m.submodule">
            <a href="{{sub_element.url}}">{{sub_element.res}}</a>
          </li>
        </ul>
      </li>
    </ul>

Javascript

    $scope.showSubmenu = function(item) {
        if ($scope.activeParentIndex === item) {
          $scope.activeParentIndex = "";
        } else {
          $scope.activeParentIndex = item;
        }
      }

      $scope.isShowing = function(index) {
        if ($scope.activeParentIndex === index) {
          return true;
        } else {
          return false;
        }
      };

      $scope.modulos = [{
        "module": "Admin System ",
        "adm_modulo_id": 1,
        "submodule": [{
          "res": "Angular",
          "url": "#/admin/1"
        }, {
          "res": "Linux",
          "url": "#/admin/2"
        }, {
          "res": "Server",
          "url": "#/admin/3"
        }]

      }];

and the result is here in a Plunk

Comments

0

Review this page http://benfoster.io/blog/angularjs-recursive-templates, they made a multinivel menu since a json structure, no matter how many level do you have. And the example http://jsfiddle.net/NP7P5/1719/

<div ng-app="app" ng-controller='AppCtrl'>
    <script type="text/ng-template" id="categoryTree">
        {{ category.title }}
        <ul ng-if="category.categories">
            <li ng-repeat="category in category.categories" ng-include="'categoryTree'">           
            </li>
        </ul>
    </script>
    <ul>
        <li ng-repeat="category in categories" ng-include="'categoryTree'"></li>
    </ul>
</div>
var app = angular.module("app", []);
app.controller("AppCtrl", function ($scope) {
$scope.categories = [
  { 
    title: "Computers",
    categories: [
      {
        title: "Laptops",
        categories: [
          {
            title: "Ultrabooks"
          },
          {
            title: "Macbooks"
          }
        ]
      },
      {
        title: "Desktops"
      },
      {
        title: "Tablets",
        categories: [
          { 
            title: "Apple"
          },
          {
            title: "Android"
          }
        ]        
      }
    ]
  },
  {
    title: "Printers"
  }
];
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.