UPDATE:
I found that if I'm passing the JSON file in controller like this:
controller('dynamicMenuCtrl', ['$scope', function ($scope) {
$scope.mainmenu2 = [
{
"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"
}
]
});
instead of getting it via $http.get
my navigation-bar is working properly. I'm calling it in my HTML as:
<li ng-repeat=item in mainmenu2>"
UPDATE 2:
If you are using MVC app, you have to register JSON file in Web.config
<system.webServer>
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json; charset=UTF-8"/>
</staticContent>
</system.webServer>
Then all will be fine
I have a problem, I have a 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"
}
]
}
I want to make a navigation bar from this JSON file. I'm using AngularJS to make this (only Angular is allowed in this case).
I made an controller to get this JSON file:
angular.module('dynamic-menu').controller('dynamicMenuCtrl', ['$scope', '$http', function ($scope, $http) {
$http.get('MenuItems.json').success(function (data) {
$scope.mainmenu = data;
});
Then I want to make an navigation bar in my HTML:
<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>
Then I'm getting an error Error:
[ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: item in mainmenu, Duplicate key: string:<, Duplicate value: "<"
I tried to use an expression trackby $index but it's not rendering in a way I want. There is a looooot of copies of something but I don't know what is wrong.
Maybe I have something wrong in my JSON file or in Controller?
EDIT: Can you see and edit this plunkr? http://plnkr.co/edit/U1xG2E4ys7SGz7WxBtvq?p=preview
It also didn't work, maybe I'm writing something wrong? I also corrected my JSON file in this post, and the error expression
EDIT2: What if I want to make the HTML via directive?
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>"
};
}]);
EDIT 3: I updated my plunkr and now it's working:
http://plnkr.co/edit/U1xG2E4ys7SGz7WxBtvq?p=preview
I created also a directive and it also working. I don't know why it's working in Plunkr, but didn't in my project :(
Repeater: item in content, Duplicate key: string:<You iterate something wrong...Repeater: item in mainmenu- I corrected this line, becausecontentwas my previous name ofmainmenu- Sorry