简体   繁体   English

带有UI-Bootstrap选项卡和UI-Router的Angularjs导航菜单

[英]Angularjs navigation menu with UI-Bootstrap tabs and UI-Router

In this Plunker , I'm unable to make menu links and tabs to work properly. 这个Plunker中 ,我无法使菜单链接和标签正常工作。 As you can see I need to click twice the 'Route 1' to go back from tabs Route2, moreover when I click twice the 'Route 2' menu link, the tabs content is not rendered. 正如您所看到的,我需要点击两次“路线1”以从标签Route2返回,而且当我点击两次“路线2”菜单链接时,标签内容不会被渲染。

I think this is the relevant part of the code that matters: 我认为这是重要的代码的相关部分:

myapp.config(function($stateProvider, $urlRouterProvider) {

  $urlRouterProvider.otherwise("/");

  $stateProvider
    .state('route1', {
      url: "/",
      templateUrl: "route1.html"
    })
    .state('DocumentoMasterView', {
      url: "/route2",
      templateUrl: "route2.html",
      controller: 'myAppController'
    })
    .state('DocumentoMasterView.A', {
      url: '/detail',
      templateUrl: 'route2.A.view.html',
      controller: 'myAppController'
    })
    .state('DocumentoMasterView.B', {
      url: '/image',
      templateUrl: 'route2.B.view.html',
      controller: 'myAppController'
    })
    .state('DocumentoMasterView.C', {
      url: '/submenu',
      templateUrl: 'route2.C.view.html',
      controller: 'myAppController'
    })

});

 myapp.controller('myAppController',['$scope','$state',function($scope, $state){
        $scope.tabs = [
           { heading: 'A View', route:'DocumentoMasterView.A', active:true},
           { heading: 'B View', route:'DocumentoMasterView.B', active:false },
           { heading: 'C View', route:'DocumentoMasterView.C', active:false }
        ];

        $scope.go = function(route){
           $state.go(route);
        };

        $scope.active = function(route){
           return $state.is(route);
        };

       $scope.$on('$stateChangeSuccess', function() {            
         $scope.tabs.forEach(function(tab) {
               tab.active = $scope.active(tab.route);
         });
       });

I've made this change to make that example working (check it here ) 我做了这个改变以使这个例子有效( 在这里查看

we do not need state change in this case 在这种情况下,我们不需要改变状态

   // instead of this
   $scope.go = function(route){
       $state.go(route);
   };

   $scope.$on('$stateChangeSuccess', function() {
       $scope.tabs.forEach(function(tab) {
           tab.active = $scope.active(tab.route);
       });
   });

we should handle all the on tab selection 我们应该处理所有选项卡选项

   // use just this
   $scope.go = function(t){
       $scope.tabs.forEach(function(tab) {
           tab.active = $scope.active(t.route);
       });
       $state.go(t.route);
   };

Check it here 在这里查看

Also, try to reconsider using of the <div ng-controller="myAppController"> with ui-router. 另外,尝试重新考虑使用ui-router的<div ng-controller="myAppController"> It could work, but with states you can define all the parts more effectively. 它可以工作,但有了状态,你可以更有效地定义所有部分。

Here I tried to show how to... no ng-controller, parent layout state... 在这里,我试图展示如何...没有ng-controller,父布局状态......

Radim's answer is great but this is an outdated/ bloated method. Radim的答案很棒,但这是一种过时/臃肿的方法。 ui-router has active states which do all these css and state changing from the view rather than having the logic in your controller. ui-router具有活动状态,它们从视图中执行所有这些css和状态更改,而不是在控制器中使用逻辑。

In your nav: 在导航中:

 <ul>
  <li ui-sref-active="active" class="item">
    <a href ui-sref="route1">route1</a>
  </li>
</ul>

And thats it! 就是这样! Your currently active state/ view will apply the ui-sref-active="active" class to that li element. 您当前活动的状态/视图将ui-sref-active =“active”类应用于该li元素。 where "active" is the class name which is applied. 其中“active”是应用的类名。

For your specific nav it would look like this: 对于您的特定导航,它看起来像这样:

<ul>
  <li ui-sref-active="active" class="item">
    <a href ui-sref="route1">route1</a>
  </li>
    <li ui-sref-active="active" class="item">
    <a href ui-sref="DocumentoMasterView">DocumentoMasterView</a>
  </li>
    <li ui-sref-active="active" class="item">
    <a href ui-sref="DocumentoMasterView.A">DocumentoMasterView.A</a>
  </li>
    <li ui-sref-active="active" class="item">
    <a href ui-sref="DocumentoMasterView.B">DocumentoMasterView.B</a>
  </li>
    <li ui-sref-active="active" class="item">
    <a href ui-sref="DocumentoMasterView.C">DocumentoMasterView.C</a>
  </li>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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