简体   繁体   English

支持路由的AngularJS UI Bootstrap选项卡

[英]AngularJS UI Bootstrap Tabs that support routing

I would like to use AngularJS UI Bootstrap Tabs in my project, but I need it to support routing. 我想在我的项目中使用AngularJS UI Bootstrap选项卡 ,但我需要它来支持路由。

For example: 例如:

Tab         URL
--------------------
Jobs       /jobs
Invoices   /invoices
Payments   /payments

As far as I can tell from the source code, the current tabs and pane directives doesn't support routing. 据我所知,源代码中,当前的tabspane指令不支持路由。

What would be the best way to add routing? 添加路由的最佳方法是什么?

To add routing you typically use an ng-view directive. 要添加路由,通常使用ng-view指令。 I'm not sure it's easy enough to modify angular UI to support what you're looking for, but here's a plunker showing roughly what i think you're looking for (it's not necessarily the best way of doing it - hopefully someone can give you a better solution or improve on this) 我不确定修改角度UI以支持你正在寻找的东西是很容易的,但这里是一个大概显示我认为你正在寻找的东西(它不一定是最好的方式 - 希望有人可以给你是一个更好的解决方案或改进这个)

Use data-target="#tab1". 使用data-target =“#tab1”。 Worked for me 为我工作

我也有这个要求,我正在做类似于Chris提供的答案,但我也使用AngularUI路由器 ,因为ngRouter不支持嵌套视图,你可能会在另一个视图中有标签内容视图(我做了),这不适用于ng-view。

这个答案对我有帮助http://odetocode.com/blogs/scott/archive/2014/04/14/deep-linking-a-tabbed-ui-with-angularjs.aspx (非常简单但功能强大的解决方案)

you could pass your own custom key value pairs in the route definition and achieve this. 您可以在路由定义中传递自己的自定义键值对并实现此目的。 here's a good example: http://www.bennadel.com/blog/2420-Mapping-AngularJS-Routes-Onto-URL-Parameters-And-Client-Side-Events.htm 这是一个很好的例子: http//www.bennadel.com/blog/2420-Mapping-AngularJS-Routes-Onto-URL-Parameters-And-Client-Side-Events.htm

Agree with the use of UI-Router which tracks states and works great with nested views. 同意使用UI-Router来跟踪状态并使用嵌套视图。 Speaking particularly of your Bootstrap tabs issue there is a great implementation that leverages UI Router: UI-Router Tabs 特别是您的Bootstrap选项卡问题,有一个很好的实现利用UI路由器: UI-Router选项卡

If you have a route called settings and you want to have tabs in that settings page something like this works. 如果您有一个名为settings的路由,并且您希望在该设置页面中有选项卡,则此类工作正常。

<div class="right-side" align="center">
    <div class="settings-body">
        <ul class="nav nav-tabs">
          <li class="active"><a data-toggle="tab" href="#!/settings#private_email">Private email</a></li>
          <li><a data-toggle="tab" href="#!/settings#signature">Signature</a></li>
          <li><a data-toggle="tab" href="#menu2">Menu 2</a></li>
        </ul>

        <div class="tab-content">
          <div id="private_email" class="tab-pane fade in active">
            <div class="row" ng-controller="SettingsController">
                <div>
                   <button class="btn btn-primary" ng-click="activatePrivateEmail()">Activate email</button>
                   <button class="btn btn-danger">Deactivate email</button>
                </div>
            </div>
          </div>
          <div id="signature" class="tab-pane fade">
              <textarea ui-tinymce="tinymceOptions" ng-model="signature"></textarea>
              <div class="send-btn">
                <button name="send" ng-click="" class="btn btn-primary">Save signature</button>
              </div>
          </div>
          <div id="menu2" class="tab-pane fade">
            <h3>Menu 2</h3>
            <p>Some content in menu 2.</p>
          </div>
        </div>
    </div>
</div>

just a small add to accepted answer: i needed to keep the current tab at page refresh so i used a switch like this: 只是一小部分添加到接受的答案:我需要保持当前选项卡在页面刷新,所以我使用这样的开关:

$scope.tabs = [
            { link : '#/furnizori', label : 'Furnizori' },
            { link : '#/total', label : 'Total' },
            { link : '#/clienti', label : 'Clienti' },
            { link : '#/centralizator', label : 'Centralizator' },
            { link : '#/optiuni', label : 'Optiuni' }
        ];

        switch ($location.path()) {
            case '/furnizori':
                $scope.selectedTab = $scope.tabs[0];
                break;

            case '/total':
                $scope.selectedTab = $scope.tabs[1];
                break;

            case '/clienti':
                $scope.selectedTab = $scope.tabs[2];
                break;

            case '/centralizator':
                $scope.selectedTab = $scope.tabs[3];
                break;

            case '/optiuni':
                $scope.selectedTab = $scope.tabs[4];
                break;

            default:
                $scope.selectedTab = $scope.tabs[0];
                break;
        }

I got tabs with routing working the following way. 我通过以下方式获得了路由选项。

It's able to do everything you want from dynamic tabs, and it's actually very simple. 它能够从动态标签中完成您想要的所有操作,而且实际上非常简单。

  • Tabs with a ui-view , so it can dynamically load templates, 带有ui-view选项卡,因此可以动态加载模板,
  • Update routing in URL 更新URL中的路由
  • Set history state 设置历史状态
  • When directly navigating to a route with a tabbed view, the correct tab is marked as active . 直接导航到带有选项卡式视图的路径时,正确的选项卡将标记为active

Define the tabs with a parameter in your router 使用路由器中的参数定义选项卡

.state('app.tabs', {
    url         : '/tabs',
    template    : template/tabs.html,
})
.state('app.tabs.tab1', {
    url         : '/tab1',
    templateUrl : 'template/tab1.html',
    params      : {
        tab         : 'tab1'
    }
})
.state('app.visitors.browser.analytics', {
    url         : '/tab1',
    templateUrl : 'template/tab2.html',
    params      : {
        tab         : 'tab2'
    }
})

The tabs template (tabs.html) is 标签模板(tabs.html)是

<div ng-controller="TabCtrl as $tabs">
    <uib-tabset active="$tabs.activeTab">
        <uib-tab heading="This is tab 1" index="'tab1'" ui-sref="app.tabs.tab1"></uib-tab>
        <uib-tab heading="This is tab 2" index="'tab2'" ui-sref="app.tabs.tab2"></uib-tab>
    </uib-tabset>
    <div ui-view></div>
</div>

Which is paired with a very simple controller for getting the current active tab: 这与用于获取当前活动选项卡的非常简单的控制器配对:

app.controller('TabCtrl', function($stateParams) {
    this.activeTab = $stateParams.tab;
})

This aught to be able to do what you want: 这要能做你想做的事:

https://github.com/angular-ui/ui-router https://github.com/angular-ui/ui-router

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

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