2017-04-10 80 views
0

我試圖在點擊特定標籤時禁用所有其他標籤。意思是,假設,最初當應用程序運行時,tab1顯示爲默認值。之後,如果我選擇tab2,則所有其他選項卡都應禁用,並且只有在單擊「取消」按鈕時纔會啓用。如何在單擊特定選項卡後禁用其他選項卡?

HTML代碼:

<tabset>  
     <tab 
      ng-repeat="t in tabs" 
      heading="{{t.heading}}" 
      select="go(t.route)" 
      active="t.active"> 
     </tab> 
    </tabset> 
    <div ui-view></div> 

JS代碼:

var app = angular.module("routedTabs", ["ui.router", "ui.bootstrap"]); 

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

     $urlRouterProvider.otherwise("/main/tab1"); 

     $stateProvider 
      .state("main", { abtract: true, url:"/main", templateUrl:"main.html" }) 
       .state("main.tab1", { url: "/tab1", templateUrl: "tab1.html" }) 
       .state("main.tab2", { url: "/tab2", templateUrl: "tab2.html" }) 
       .state("main.tab3", { url: "/tab3", templateUrl: "tab3.html" }) 
       .state("main.tab4", { url: "/tab4", templateUrl: "tab4.html" }); 

    }); 

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

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

     $scope.tabs = [ 
      { heading: "Tab1", route:"main.tab1", active:false }, 
      { heading: "Tab2", route:"main.tab2", active:false }, 
      { heading: "Tab3", route:"main.tab3", active:false }, 
      { heading: "Tab4", route:"main.tab4", active:false }, 
     ]; 

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

您可以創建一個小提琴/ plunker? – tanmay

+0

[plunker](http://plnkr.co/edit/EIDvQusuT0zLixo6xxBy?p=preview)我已經創建了plunker –

+0

,所以取消按鈕位於當前標籤內還是在所有標籤之外? – tanmay

回答

0

請找到下面的代碼片段,另外加入onSelectTab()。讓我知道你是否需要改變。
DEMO

HTMLFILE

<div ng-controller="mainController"> 
    <tabset> 
     <tab 
      ng-repeat="t in tabs" 
      heading="{{t.heading}}" 
      select="go(t.route, true)" 
      ng-click="onSelectTab(t)" 
      active="t.active" 
      disabled="flags.disableTab && !t.active"> 
     </tab> 
    </tabset> 
    <button class="btn" ng-click="flags.disableTab = false">CANCEL</button> 
    <div ui-view></div> 
</div> 

controllerFile.js

app.controller("mainController", function($rootScope, $scope, $state, $timeout, $filter) { 

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

//Additionally added below method 
$scope.onSelectTab = function(t) { 
    if (t.heading == 'tab2') { 
     $scope.flags.disableTab = true; 
     } 
}; 
//End 

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

    $scope.tabs = [{ 
    heading: "tab1", 
    route: "main.tab1", 
    active: false 
    }, { 
    heading: "tab2", 
    route: "main.tab2", 
    active: false 
    }, { 
    heading: "tab3", 
    route: "main.tab3", 
    active: false 
    }, { 
    heading: "tab4", 
    route: "main.tab4", 
    active: false 
    }, ]; 

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

注:接受的答案,如果它的工作原理

+0

非常感謝@Srigar。有效。但是我有一點不同的要求。取消按鈕位於第二個選項卡內,位於其他html中。我從你的解決方案中獲得了幫助,並且工作。再次感謝。 –

+0

僅維護父控制器中的標誌。它會工作。如果有用,請接受答案。 – Srigar

相關問題