0

I'm trying to create a navbar that will appear on every page, by using ng-include to reference it. The navbar works by itself, but for some reason it isn't showing up on the page(s) that I am trying to ng-include it on (for example, the dashboard). I am not using bootstrap because I'm trying to do it from scratch, for learning purposes. I feel like I'm possibly not understanding how AngularJS works?

Here is the code I have so far:

dashboard.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel='stylesheet' href='dashboard.css'>
    <title>Dashboard</title>
  </head>
<body ng-app='MyApp'>

    <nav class="navbar"><div ng-include src="'navbar.html'"></div><p>Hi!</p></nav>

<div class='dashboard'>
  Here be contents
</div>

  <script src="bower_components/angular/angular.js"></script>
  <script src="bower_components/angular-route/angular-route.js"></script>
  <script src="app.js"></script>
  <script src="view1/view1.js"></script>
  <script src="view2/view2.js"></script>
  <script src="components/version/version.js"></script>
  <script src="components/version/version-directive.js"></script>
  <script src="components/version/interpolate-filter.js"></script>
</body>
</html>

navbar.html:

<!DOCTYPE html>
<html >
  <head>
    <meta charset="utf-8">
    <link rel='stylesheet' href="navbar.css">
  </head>
<body ng-app='MyApp'>

<nav class='navbar'>
  <ul>
    <li><a href="../app/dashboard/dashboard.html">Home</a></li>  
    <li><a href="app/other.html">Temp</a></li>


  </ul>    




</nav>


  <script src="bower_components/angular/angular.js"></script>
  <script src="bower_components/angular-route/angular-route.js"></script>
  <script src="app.js"></script>
  <script src="view1/view1.js"></script>
  <script src="view2/view2.js"></script>
  <script src="components/version/version.js"></script>
  <script src="components/version/version-directive.js"></script>
  <script src="components/version/interpolate-filter.js"></script>
</body>
</html>

navbar.css:

.navbar {
    font-family: Arial, sans-serif;
    font-size: .9em;
}
.navbar ul {
    list-style-type: none;
    overflow: hidden;
    margin: 0;
    padding: 0;
    background-color: lightslategray;
    position: fixed;
    top: 0;
    width: 100%;
}
.navbar li {
    float: left;    
}
.navbar a {
    display: block;
    text-decoration: none;
    cursor: pointer;
    padding: 14px 16px;
    text-align: center;
    background-color:lightslategray;
    color: white;
}
.navbar a:hover {
    background-color: darkslategrey;
}
.active {

}
8
  • HTML with a body tag can not be nested. Commented Mar 9, 2016 at 21:42
  • Could you elaborate a bit more on what that means? Commented Mar 9, 2016 at 21:46
  • ng-include="string" from ng-include Commented Mar 9, 2016 at 21:46
  • I tried removing the single quotes but it still didn't work, unfortunately. Commented Mar 9, 2016 at 21:48
  • You are trying to include something that has a body tag inside an existing body element. There can only be one body in an HTML document. Commented Mar 9, 2016 at 21:49

3 Answers 3

1

Your navbar template shouldn't be an entirely new HTML page. You are just injecting markup, so that file should include code as it would be written.

navbar.html should ONLY consist of

<nav class='navbar'>
  <ul>
    <li><a href="../app/dashboard/dashboard.html">Home</a></li>  
    <li><a href="app/other.html">Temp</a></li>    
  </ul>
</nav>
Sign up to request clarification or add additional context in comments.

3 Comments

Hmm...I tried that, however the navbar is still not showing up on the dashboard.html? I put the navbar.css contents into dashboard's css file.
Your navbar should be showing up, it just isn't visible because you never gave it a background color. Add something like "background-color: #eee" to the .navbar class in your css and it should show up
That fixed it! Thanks a ton! Just curious though, why does the navbar show up inside navbar.html even without a background color, but it doesn't show up without the background color in dashboard.html?
0

Try removing the single quotes around navbar.html and also make sure that navbar.html exists in the same directory as dashboard.html. If it's in a different directory, you must specify.

5 Comments

I've read that the single quotes are necessary apparently? And navbar.html is in the same directory as dashboard.html.
I'm not 100% positive but I wouldn't think the single quotes are necessary. AngularJS documentation doesn't show any single quotes. Worth a try to take them out.
I saw that AngularJS documentation as well, so I did try taking them out already but that didn't fix the problem, unfortunately.
Hmm. And you're 100% positive that navbar.html lives in the app/dashboard dir?
Yep, it's there, along with navbar.css.
0

First, ng-include must only have the code you need in that part, for example just <nav> </nav> tag.
Second, because you didn't add navbar.css on dashboard.html that is the reason why the navbar doesn't show.

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.