简体   繁体   中英

using ng-repeat with the bootstrap navbar element

I am trying to make a nav bar using bootstrap.Following is the code.

<div>
   <ul class="nav nav-tabs">
      <li role="presentation" class="active"><a href="#">Home</a></li>
      <li role="presentation" class="active"><a href="#">Option 1</a></li>
      <li role="presentation" class="active"><a href="#">Option 2</a></li>
   </ul>
</div>

I want to use the ng-repeat for the options. I tried the following code:

<div ng-controller="MainController" ng-repeat="option in navBarOptions">
   <ul class="nav nav-tabs">
      <li class="active" role="presentation"><a href="#">{{ option.optionText }} </a></li>
   </ul>
</div>

Here's the JS part.

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

app.controller('MainController',['$scope', function($cope) {
$cope.navBarOptions = [
   {
      optionText: 'Home',
   },
   {
      optionText: 'Option 1',
   },
   {
      optionText: 'Option 2',
   },
];
}]);

Thanks. I am fairly new to this domain.Let me know if i missed something very trivial.

You need to repeat the li elements , not the div where the ng-controller resides:

<div ng-controller="MainController">
    <ul class="nav nav-tabs">
        <li role="presentation" ng-repeat="option in navBarOptions">
            <a href="#">{{option.optionText}}</a>
        </li>
    </ul>
</div>

NB: Looking at the generated DOM would have gave you a clue about what is wrong with your code.

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