Create Menu & Submenu using json data




index.php


<html ng-app="satyamApp">
<head>
    <title>Angularjs Menu</title>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script src="app.js"></script>
</head>

<body>

    <coutom-menu> </coutom-menu>

</body>

</html>

app.js

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

    app.directive('coutomMenu', function () {
        return { controller: function ($scope, $http) {
                $http.get('data.json').success(function (data) {
                    $scope.menu = data;
                    $scope.submenu = data[0].sub;
                });
                $scope.changeSub = function (menuId) {
                    $scope.submenu = $scope.menu[menuId].sub;
                }
            },
            restrict: 'E',
            templateUrl: 'nav.html'
        };

    });

nav.html

<nav>
<ul class="drop_menu">
<a href="#{{item.link}}" ng-repeat="item in menu" ng-click="changeSub(item.id)"><li>{{item.name}}</li></a>
<ul>
<a href="#{{item.link}}" ng-repeat="item in submenu"><li>{{item.name}}</li></a>
</ul>
</li>
    </ul>
</nav>

data.json

[
    {
        "id": "0",
        "name": "Home","link":"home"
    },
    {
"id": "1",
     "name": "About",
     "link": "about"
    },
    {
        "id": "2",
        "name": "Services",
        "sub": [
            {"name": "Services 1","link": "services 1"},
            {"name": "Services 2","link": "services 2"},
            {"name": "Services 3","link": "services 3"},
            {"name": "Services 4","link": "services 4"}
        ]
    },
    {
        "id": "3",
        "name": "Item-4",
        "sub": [
            {"name": "SItem 4-1"},
            {"name": "SItem 4-2"},
            {"name": "SItem 4-3"},
            {"name": "SItem 4-4"}
        ]
    }

]








Previous
Next Post »