简体   繁体   中英

Angular Nav Bar

I am trying to turn my bootstrap navbar into an angular element so that I can change the attributes in the controller rather than rewriting every page. The navbar won't show though and it's not throwing any errors

HTML call

 <body ng-app="appHeaderApp">
  <div class="main" ng-controller="appHeaderController">
  <div ng-repeat="tab in tabs">
  <app-header info="tab"></app-header>
  </div>

appHeaderApp

var app = angular.module("appHeaderApp", []);

appHeaderController

app.controller('appHeaderController', ['$scope', function($scope) { 
  $scope.myNavTabs = [
{ 
    tab1: 'link',
  tab2: 'more links',
  tab3: 'different link',
  tab4: 'another link',
  tab5: 'last link'
}
 ];
   /*$scope.returnEvents = function(index) { 
  'I have been in,' + $scope.myinfo[index].userEvents; 
 };*/
 }]);

headerjs.html

  <nav class="navbar navbar-default">
 <div class="container-fluid">
  <div class="navbar-header">
     <button type="button" class="navbar-toggle collapsed" data-   toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
     <span class="icon-bar"></span>
     <span class="icon-bar"></span>
     </button>
     <a class="navbar-brand" href="#">Brand</a>
     <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav">
           <li class="active"><a href="#"> {{info.tab1}} <span class="sr-only">(current)</span></a></li>
           <li><a href="#"> {{info.tab2}} </a></li>
           <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> {{info.tab3}} <span class="caret"></span></a>
              <ul class="dropdown-menu">
                 <li><a href="#"> {{info.tab4}} </a></li>
                 <!-- put the login form here -->
              </ul>
           </li>
        </ul>
     </div>
  </div>

appHeader.js

app.directive('appHeader', function() { 
 return { 
restrict: 'E', 
scope: { 
  info: '=' 
}, 
templateUrl: 'js/directives/headerjs.html' 
   }; 
 });

Your ng-repeat should have <div ng-repeat="tab in myNavTabs"> instead of tabs since you have assigned your array to $scope.myNavTabs and not $scope.tabs.


$scope.myNavTabs = [
{ 
    tab1: 'link',
  tab2: 'more links',
  tab3: 'different link',
  tab4: 'another link',
  tab5: 'last link'
}
 ];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM