6

I've created an angular application that uses Twitter bootstrap.

I would like dynamic links in the navbar (like ng-href="#/{{course.id}}"

They never render - they end up like href="#/"

If I put the navbar inside the <div ng-view> section of the page they do render, although I'd rather not put it there since I don't want to duplicate it on each partial.

The basic page is laid out like this:

<html ng-app="studentApp">
  <head> links to cdns here</head>
  <body>
    <div class="container-fluid">
      <div class="navbar navbar-inverse navbar-fixed-top">Dynamic bar here</div>
      <div ng-view>Partials in here</div>
    </div>
  </body>
</html>

For all partials that load in the ng-view portion, the interpolation happens as expected, but not in the navbar.

I created a Plunker rather than trying to include everything here. (http://plnkr.co/MK8QEDQUVawkOi92xJXk).

1
  • removing height:0 and overflow:hidden from .nav-collapse, .nav-collapse.collapse class shows the nav bar.. Commented May 1, 2013 at 20:41

1 Answer 1

6

Angular will only work inside the ng-view once you start using routing.

I have only this in the containing page:

// links to cdn, js, css
<body>
  <div id="app-container" ng-app="dashboard" ng-view></div>
</body>

All the partials show up inside the div.

All partials include the navigation partial at the very top:

<div ng-include src="'html/navigation.html'"></div>

navigation.html has its own controller:

<div id="nav-bars-container" ng-controller="NavCtrl">
  <a href="#/{{course.id}}">Foo</a>
</div>

& thus has its own scope. Others can be injected:

function NavCtrl($scope, $route, $routeParams) {
  console.log($routeParams);
  $scope.course.id = $routeParams.id;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, @flim. This is very helpful. I did not know about ng-include. The addition of the separate controller in the navbar helped me better understand controllers and routes. I've updated the plunker (plnkr.co/edit/MK8QEDQUVawkOi92xJXk) with the changes (except the app location). The navbar picks up the routeParams and behaves like I want it to. Per rajkamal I removed the responsive components from the style so the entire nav would render in the preview mode (rather than fullscreen), though it does wrap in a small width.
You're welcome! It was a brainteaser for me as well. Took me a bit to figure this pattern. I'm open to more elegant (DRY) solutions other than ng-include the navigation.html in all partials, but it is an easy solution.
For clarification (for me and future readers) you can't just use a partial view once and have it persist, it needs to be used on each page is that correct?
I don't understand what you mean by "have it persist." You can wrap a ng-if or ng-show around the <div ng-include..> if you want the nav bar to show up conditionally.
I think I understand what you mean.. The nav bar needs to be included in each partial in this pattern. That's why I commented that I'm open to a more elegant solution that's as simple..

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.