1

I created a json File with an array of "learnobjects". Every object has an id, a name, and a link.

Here is my plnkr example

var myMessage = {
"learnobjects": [{
    "id": "1",
    "name": "Animation-Basics",
    "link": "animation_basics.html",

},    
{
    "id": "2",
    "name": "Interpolation",
    "link": "interpolation.html",

}
]}

var jsonData = JSON.parse(myMessage);
for (var i = 0; i < jsonData.learnobjects.length; i++) {
var id = jsonData.myMessage[i].id;
var name = jsonData.myMessage[i].name;
var link = jsonData.myMessage[i].link;


var mainPath = document.createElement('a');
    mainPath.className = 'w3-bar-item w3-mobile w3-border-right';
    mainPath.id = id;
    mainPath.href = link;
    mainPath.innerHTML =
    name + '</a>';
    document.getElementById('myHeader').appendChild(mainPath);
}
var myApp = angular.module('myApp', []);

function HeaderCtrl($scope) {
$scope.header = {name: "header.html", url: "header.html"};
}

Index.html:

<!DOCTYPE html>
<html ng-app="myApp">

<head>
<link rel="stylesheet" href="style.css">
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> 
</script>
<script src="script.js"></script>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>

<body>
<div ng-controller="HeaderCtrl">
    <div ng-include src="header.url"></div>
    <script type="text/ng-template" id="header.html"></script>
  </div>
  <p> Index </p>
</body>

</html>

Without the angular JS ($scope) it worked but every time im clicking on a link in the header, the elements are loading again everytime. I am trying to do it with AngularJS, but unfortunately it is not working yet. My problem is that I want this header to be there the whole time, and not to load every time I click on a link in it.

I would be thankful for every help or suggestion!

Cheers!

1
  • 1
    The AngularJS mechanism for achieving your goal is called a Component. You are working against AngularJS so it is giving you a headache in return. Work with the framework and you'll be more successful.docs.angularjs.org/guide/component Commented May 11, 2018 at 21:28

1 Answer 1

1

Look into <ng-view>

Elements outside of ng-view in your index will remain throughout the application thus loaded once. You can control what populates within ng-view by using angular routing. You should route your partials to load within that.

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

2 Comments

Thank you very much! Is a dynamic routing possible, if yes, then my problem would be solved :)
If AngularJS would take it's input from a json file for the links

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.