Reputation: 295
I'm thinking how to solve this problem. Let's start:
I have a menu-bar (or navigation bar) in my site.
I'm creating it from JSON file, I'm using an AngularJS directive and the template to create it:
JSON file:
{
"mainmenu": [
{
"id": "bananas",
"title": "Bananas",
"href": "#/bananas",
"li-class": "menu-element"
},
{
"id": "apples",
"title": "Apples",
"li-class": "dropdown"
"submenu": [
{
"id": "apple-lot",
"title": "Apples lots",
"href": "#/apples/lot"
},
{
"id": "apple-series",
"title": "Apples series",
"href": "#/apples/series"
}
]
},
{
"id": "cherries",
"title": "Cherries",
"href": "#/cherries",
"li-class": "menu-element"
}
]
}
My AngularJS directive with template:
angular.module('dynamic-menu').directive('menuTemplate', function () {
return {
restrict: 'E',
template: "<nav class=\"navbar navbar-inverse navbar-fixed-top\" role=\"navigation\" id=\"nav-bar\" style=\"margin-bottom: 0.5%\">"
+ "<div class=\"container-fluid\">"
+ "<div class=\"navbar-header\">"
+ "<span class=\"navbar-brand\">TITLE</span>"
+ "</div>"
+ "<div class=\"collapse navbar-collapse\">"
+ "<span class=\"navbar-brand\">"
+ "<ul class=\"nav navbar-nav\">"
+ "<li ng-repeat=\"item in mainmenu\">"
+ "<a href=\"{{item.href}}\">{{item.title}}</a>"
+"</li>"
+"</ul> <!-- /.nav navbar-nav -->"
+"</span> <!-- /.navbar-brand -->"
+"</div> <!-- /.navbar-collapse-->"
+"</div> <!-- /.container-fluid-->"
+"</nav>"
};
}]);
And AngularJS controller:
angular.module('dynamic-menu').controller('dynamicMenuCtrl', ['$scope', '$http', function ($scope, $http) {
$http.get('MenuItems.json').success(function (data) {
$scope.mainmenu = data;
});
The problem is - how to dynamically change the TITLE?
This line in my AngularJS directive:
+ "<span class=\"navbar-brand\">TITLE</span>"
Maybe I can add something to my JSON file and check in some way is there a class assigned for the active-menu-element? (for ex. <li class="menu-item-active">
) or how?
Here is Plunker - and there is a Title
which I want to change dynamically in my template
http://plnkr.co/edit/U1xG2E4ys7SGz7WxBtvq?p=preview
I have to use the directive
EDIT: What I want to achieve:
On the main page
I want to have title fruits
, when I'm on the sub-menu Bananas
I want to display the title Bananas
Upvotes: 0
Views: 1393
Reputation: 1279
json:
{
"title": "some title",
"mainmenu": [
{
"id": "bananas",
"title": "Bananas",
"href": "#/bananas",
"li-class": "menu-element"
},
{
"id": "apples",
"title": "Apples",
"li-class": "dropdown"
"submenu": [
{
"id": "apple-lot",
"title": "Apples lots",
"href": "#/apples/lot"
},
{
"id": "apple-series",
"title": "Apples series",
"href": "#/apples/series"
}
]
},
{
"id": "cherries",
"title": "Cherries",
"href": "#/cherries",
"li-class": "menu-element"
}
]
}
in template
"<span class=\"navbar-brand\">{{title}}</span>"
js: angular.module('dynamic-menu').controller('dynamicMenuCtrl', ['$scope', '$http',
function ($scope, $http) {
$http.get('MenuItems.json').success(function (data) {
$scope.mainmenu = data;
$scope.title = data.title;
});
http://plnkr.co/edit/LvObdZB72umVPCfTsVg1?p=preview
Upvotes: 1