繁体   English   中英

具有不同控制器的选项卡

[英]Tab with different controllers

在我的angular.js应用程序中,我有一个带有各种选项卡的配置文件视图,每个选项卡都是一个嵌套视图,带有自己的控制器。 我想做的是能够单击来回不同的选项卡,但始终保留在配置文件视图中。

但是,每次我单击其中一个选项卡,例如带有URL“ / nested_view_1”和templateUrl:“ nested_view_1.html”的“ nested_view_1”时,它将路径更改为“ / nested_view_1”并呈现空白页面(因为我没有在$routeProvider指定了“ / nested_view_1”)。 只有当我单击返回时,它才会在视图内显示我选择的选项卡内容

我可能同时使用ui.routerng-route可能是一个问题。

这是我的app.js文件:

var app = angular.module('app', ['ngRoute', 'ui.router','ui.bootstrap','ui.bootstrap.tpls'])     

app.config(['$routeProvider', '$stateProvider' ,function ($routeProvider, $stateProvider) {

  $routeProvider
    .when('/', {
      templateUrl: '/app/views/home.html',
      controller: 'HomepageCtrl'
    })
    .when('/articles', {
      templateUrl: 'app/views/articles/index.html',
      controller: 'ArticlesCtrl'
    })
    .when('/articles/:id', {
      templateUrl: 'app/views/articles/show.html',
      controller: 'ShowArticleCtrl'
    })
    ... other routes ...

    $stateProvider
      .state('profile', {
        abstract: true,
        url: '/profile',
        templateUrl: "app/views/users/profile.html",
        controller: 'UserShowCtrl'
      })
     .state('nested_view_1', {
       url: '/nested_view_1',
       views: {
         "tabContent": {
           templateUrl: 'app/views/users/sales.html',
           controller: 'UserSalesAreaCtrl'
         }
       }
     })
     .state('nested_view_2', {
       url: "/nested_view_2",
       views: {
         "tabContent": {
           templateUrl: 'app/views/users/buys.html',
           controller: 'UserAreaCtrl'
         }
       }
     })
     .... all the way to nested view 4, in my case
}])

我的观点:

<ul class="nav nav-tabs mt-50" >
  <li ng-repeat="t in tabs" class="nav-item" heading="{{t.heading}}"  active="t.active">
    <a ui-sref="{{t.route}}" style="font-weight: 200;" class="nav-link" ng-class="{'active'}" > 
      {{ t.heading }}
    </a>
  </li>
  <div ui-view="tabContent"></div>
</ul>

我的UserShowCtrl

angular.module('app').controller('UserShowCtrl', ['$scope', 'user', '$routeParams', '$location', '$state',
  function ($scope, user, $routeParams, $location, $state) { 

  $scope.tabs = [
    { heading: 'nested_view_1', route:'nested_view_1', active:true  },
    { heading: 'nested_view_2', route:'nested_view_2', active:false },
    { heading: 'nested_view_3', route:'nested_view_3', active:false },
    { heading: 'nested_view_4', route:'nested_view_4', active:false }
  ];

}]);

嵌套路由以父路由为父前缀。

$stateProvider
      .state('profile', {
        abstract: true,
        url: '/profile',
        templateUrl: "app/views/users/profile.html",
        controller: 'UserShowCtrl'
      })
     .state('profile.nested_view_1', {
       url: '/nested_view_1',
       views: {
         "tabContent": {
           templateUrl: 'app/views/users/sales.html',
           controller: 'UserSalesAreaCtrl'
         }
       }
     })
     .state('profile.nested_view_2', {
       url: "/nested_view_2",
       views: {
         "tabContent": {
           templateUrl: 'app/views/users/buys.html',
           controller: 'UserAreaCtrl'
         }
       }
     })

暂无
暂无

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

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