0

I'm trying to display navigation bar menu items using ng-repeat and I'm using ui router simultaneously, but output is not what I expected.

<!--HTML-->
 <body>
 <div ng-app="navApp" ng-controller="navCtrl">
            <a  ng-repeat="item in navItems" ui-sref="{{item.sref}}">{{item.label}}</a>   
  </div>
  <div ng-app="mainApp">
      <div ui-view></div>
  </div>
 </body>

//JS
angular.module('navApp', []).controller('navCtrl', function($scope){
    $scope.navItems = [
        {'sref': 'home', 'label': 'Home'},
        {'sref': 'login', 'label': 'Login'},
        {'sref': 'signup', 'label': 'Signup'},
        {'sref': 'other', 'label': 'Label'}
    ];
});  


var app = angular.module('mainApp', ['ui.router']);

app.config(function($stateProvider, $urlRouterProvider){
//   $urlRouterProvider.otherwise('/');

   $stateProvider.state('login', {
      url: '/login',
      templateUrl:'login.html',
      controller: function($scope) {
          $scope.message = 'Login Page'                 
      }
   }).state('signup', {
       url: '/signup',
       templateUrl:'signup.html',
       controller: function($scope) {
           $scope.status = 'Signup Page'
       }
   }).state('home', {
       url: '/',
       template:'Home Page'
   }).state('other', {
       url: '/other',
       template:'Other Page'
   });
});

I'm getting output as:

HomeLoginSignupLabel

Here sref are not working as href. Using static navigation menu instead of displaying dynamically then everything is working fine. I think both apps are executing at same causing this issue.

1 Answer 1

2

use some navbar like bootsrap or semantic-ui etc.

bootstrap example :

 <body>
      <div ng-app="navApp" ng-controller="navCtrl">
         <nav class="navbar navbar-inverse">
            <ul class="nav navbar-nav">
                <li ng-repeat="item in navItems"><a ui-sref="{{item.sref}}">{{item.label}}</a></li>
            </ul>
          </nav>
      <div ng-app="mainApp">
         <div ui-view></div>
      </div>
</body>
Sign up to request clarification or add additional context in comments.

1 Comment

Now it looks well .

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.