【问题标题】:AngularJS Directive For UI BootStrap Tabs (Isolate Scope Issues)UI BootStrap 选项卡的 AngularJS 指令(隔离范围问题)
【发布时间】:2015-07-06 16:26:23
【问题描述】:

我正在尝试制定一个指令来使用 Angular UI 引导选项卡。
该指令的主要目的是记住已选择的选项卡。
我已经查看了所有的解决方案,但似乎没有一个可以解决未在控制器中设置的选项卡的问题:

<tabset>
  <tab heading="Customers" sctab>Customer tab content</tab>
  <tab heading="Sales" sctab>Sales Tab Content</tab>
</tabset>

注意“sctab”是我的指令名称。在这里我有以下工作:

  • 点击一个选项卡,它会设置 url 哈希作为选项卡标题名称
  • 如果您单击链接然后返回,则会记住该哈希值。
  • 在页面加载时识别与哈希匹配的选项卡(或元素)

但是,我在最后一步遇到了障碍:

  • 激活与 url 哈希匹配的选项卡

为此,我尝试了几件事,您可以在下面的代码中看到。没有一个完全奏效。

我相信活动状态是在 ui 引导选项卡指令的隔离范围变量中设置的,称为“活动”。

ngClass 也绑定到这个变量:

ng-class="{active: active, disabled: disable}"

所以基本上在这里我一直在尝试为选项卡设置 active = true 以尝试激活选项卡(无济于事)。
在详细介绍之前,我将在这里停下来看看是否有人根据我的尝试有想法。
这是我的指令:

angular.module('jonsmodule')
.directive('sctab', ['$rootScope', '$state', '$location', function($rootScope, $state, $location) {
    return {
        restrict: "A",
        link: function (scope, element, attrs) {

            /**
             * INITIALIZING THE ACTIVE TAB FROM HASH
             */
            scope.$watch(
                // watch for setting of the element heading (so we know when the bootstrap tabs are loaded)
                function () { return element.children().first('p')[0].innerText; },
                function (newValue, oldValue) {
                    // when ui-bootstrap tabs loaded
                    if (newValue !== oldValue) {

                        // if hash matches the tab heading name
                        if ($location.hash() == newValue.trim()) {

                            /**
                             * ******** NEED HELP HERE ************
                             * THE GOAL HERE IS TO SET THE TAB ACTIVE
                             * it does not work completely
                             * below is a list of things I tried
                             */

                            //$(element).tab('show'); // .tab is not a function

                            $(element).addClass('active'); // changes the class but the tab will not change

                            scope.$$childHead.active = true; // appears to not save

                            // attrs.$set('ngClass', {active: true, disabled: false}); // this does not appear to change anything

                        } else {
                            scope.$$childHead.active = false; // appears to save and the first tab is not selected
                        }

                    }

                }
            );

            /**
             * CHANGING THE TAB
             */
            // get the state (controller)
            var current_state = $state.current.name;

            // on tab change set the hash
            element.on('click', function () {
                var tabLabel = element.children().first('p')[0].innerText;
                history.pushState(null, null, current_state + '#' + tabLabel);
            });
        },
    };
}]);

【问题讨论】:

    标签: angularjs angular-ui-bootstrap isolate-scope angular-ui-bootstrap-tab


    【解决方案1】:

    尝试在选项卡本身中设置选项卡的活动状态并将其指向范围值。例如:

      <tab select="tabSelected('tab1')" active="tabs.tab1">
      <tab select="tabSelected('tab2')" active="tabs.tab2">
    
    
       $scope.tabs = {tab1: false, tab2:false};
       $scope.tabSelected = function (whichTab) {
    
            $scope.currentTab = whichTab;
       };
    
       function setTabDisplayed() {
            $scope.tabs[$scope.currentTab] = true;
        }
    

    【讨论】:

    • 感谢您的回复。我可以走这条路,但关键是要制定指令,所以我只需要这样做一次,而不必更新我们平台的每个页面/控制器。
    • 我实际上是在表明你应该在你的指令中这样做。
    猜你喜欢
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 2012-12-27
    相关资源
    最近更新 更多