【问题标题】:Tabs are not working properly after including angular in my template在我的模板中包含角度后,选项卡无法正常工作
【发布时间】:2016-01-26 07:32:47
【问题描述】:

因为我在 HTML 引导模板中包含了 AngularJS,所以我遇到了选项卡问题。我正在使用Flatastic template。现在我不知道如何进一步进行。

让我给你看我的html代码

<div>
    <!--tabs navigation-->
    <nav>
        <ul role="tablist">
            <li role="tab" tabindex="0" aria-controls="tab-1" aria-labelledby="ui-id-1" aria-selected="true">
               <a href="#tab-1" role="presentation" tabindex="-1" id="ui-id-1">Tab 1</a>
            </li>
            <li role="tab" tabindex="-1" aria-controls="tab-2" aria-labelledby="ui-id-2" aria-selected="false">
                <a href="#tab-2" role="presentation" tabindex="-1" id="ui-id-2">Tab 2</a>
            </li>
            <li role="tab" tabindex="-1" aria-controls="tab-3"  aria-labelledby="ui-id-3" aria-selected="false">
                <a href="#tab-3" role="presentation" tabindex="-1" id="ui-id-3">Tab 3</a>
            </li>
        </ul>
    </nav>
    <!--tabs content-->
    <section class="tabs_content shadow r_corners">
        <div id="tab-1" aria-labelledby="ui-id-1" role="tabpanel" aria-expanded="true" aria-hidden="false">
            <p>BLABLABLA</p>
        </div>
        <div id="tab-2" aria-labelledby="ui-id-2" role="tabpanel" aria-expanded="false" aria-hidden="true" style="display: none;">
            <p>BLABLABLA </p>
        </div>
        <div id="tab-3" aria-labelledby="ui-id-3" role="tabpanel" aria-expanded="false" aria-hidden="true" style="display: none;">
            <p>BLABLABLA</p>
        </div>
    </section>
</div>

正如您所见,标签可以通过标签&lt;a href="#tab3" ... 进行点击,但问题是 AngularJs 接管了链接并将您重定向到其他页面。我相信这些选项卡是用 Jquery 编写的,现在我想知道我需要做什么才能使其按预期工作。 如果您需要任何其他信息,请告诉我,我会提供。

+ 当您在 Bootstrap 模板中包含 Angular 以及如何面对这些问题时,找到任何文章、教程或任何类型的信息的用户可以获得奖励积分

【问题讨论】:

  • 你可以格式化你的代码以获得更好的可读性..
  • 我已经从 HTML 中删除了一些不必要的数据

标签: javascript jquery html angularjs twitter-bootstrap


【解决方案1】:

你可以使用这个angularJS组件示例来创建tabs

角度文件 (.js)

angular.module('components', [])

  .directive('tabs', function() {
    return {
      restrict: 'E',
      transclude: true,
      scope: {},
      controller: function($scope, $element) {
        var panes = $scope.panes = [];

        $scope.select = function(pane) {
          angular.forEach(panes, function(pane) {
            pane.selected = false;
          });
          pane.selected = true;
        }

        this.addPane = function(pane) {
          if (panes.length == 0) $scope.select(pane);
          panes.push(pane);
        }
      },
      template:
        '<div class="tabbable">' +
          '<ul class="nav nav-tabs">' +
            '<li ng-repeat="pane in panes" ng-class="{active:pane.selected}">'+
              '<a href="" ng-click="select(pane)">{{pane.title}}</a>' +
            '</li>' +
          '</ul>' +
          '<div class="tab-content" ng-transclude></div>' +
        '</div>',
      replace: true
    };
  })

  .directive('pane', function() {
    return {
      require: '^tabs',
      restrict: 'E',
      transclude: true,
      scope: { title: '@' },
      link: function(scope, element, attrs, tabsController) {
        tabsController.addPane(scope);
      },
      template:
        '<div class="tab-pane" ng-class="{active: selected}" ng-transclude>' +
        '</div>',
      replace: true
    };
  })

此链接可以提供更多想法。看本页最后一个例子https://angularjs.org/

【讨论】:

  • 你能提供一些例子吗
  • 另一种选择。也许本教程可以帮助你。 grobmeier.de/bootstrap-tabs-with-angular-js-25112012.html
  • 已经尝试过了,得到错误 $(...).tab is not a function。问题是因为该模板使用了大量的 Jqeury 代码,现在我不知道如何进一步进行。我应该重写所有的 JQuery 代码还是什么?
【解决方案2】:

问题在于 AngularJs 将其作为路由:href="#tab-1"。

 <li role="tab" tabindex="0" aria-controls="tab-1" aria-labelledby="ui-id-1" aria-selected="true">
           <a href="#tab-1" role="presentation" tabindex="-1" id="ui-id-1">Tab 1</a>
 </li>

你可以试试这个,我认为这是在同一个项目中使用 Bootstrap 和 AngularJs 没有任何冲突的最佳解决方案:Angular Ui Bootstrap

使用非常简单,就是一个例子和tab的plunker的教程:tabs with angular ui bootstrap

希望对你有所帮助。

【讨论】:

  • 是的,我看到了那个例子,但它对我不起作用。我学到的是,在已经创建的带有大量 jquery 的模板中包含 angular 是很痛苦的。感谢您的建议!
  • 我已经借助上层代码解决了这个问题,所以我将继续处理其他问题。 ;) 无论如何,谢谢你的热情
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多