2017-04-24 74 views
1

我是Angular的新手,正試圖製作一個標籤控件,爲每個標籤(動態地)加載一個狀態。帶UI路由器和UI-Bootstrap的角標籤

我的標籤代碼:

<uib-tabset> 
     <uib-tab index="$index + 1" ng-repeat="tab in tabData" heading="{{tab.heading}}" disable="tab.disabled"> 
      <a ui-sref="{{tab.route}}">Click me</a> 
     </uib-tab> 
    </uib-tabset> 

我正在注射在那裏:

$scope.tabData = [ 
{ 
    heading: 'Companies', 
    route: 'LandingTemplate.Company', 
    disabled: false 
}, 
{ 
    heading: 'Users', 
    route: 'LandingTemplate.Users', 
    disabled: false 
}, 
{ 
    heading: 'Clients', 
    route: 'LandingTemplate.Clients', 
    disabled: true 
} 
]; 

樣品路線:

.state('LandingTemplate.Company', 
{ 
    url: '/company', 
    views: { 
     '[email protected]': { 
      templateUrl: 'App/Views/Admin.html' 
     }, 
     '[email protected]': { 
      templateUrl: 'App/Views/Company.html' 
     } 
    } 
}) 

它目前接線方式是單擊選項卡,點擊鏈接,這將呈現您的觀點。我只想點擊該標籤。任何幫助,將不勝感激。

+0

我希望我的UIB標籤帶我去那個狀態,用戶界面,SREF是在那裏一個例子。該選項卡會在理想情況下觸發ui-sref,但在選項卡語法上沒有屬性可以這樣做。 – RandomUs1r

回答

1

您可以在ui-tab內添加uib-tab-heading指令,其中a(定位)標記爲ui-sref並且保持標籤內容爲空。這將使您的標籤作爲錨標籤,這將幫助您重定向到其他狀態。

<uib-tabset> 
    <uib-tab index="$index + 1" ng-repeat="tab in tabData" disable="tab.disabled"> 
     <uib-tab-heading> 
     <a ui-sref="{{tab.route}}">Click me</a> 
     </uib-tab-heading> 
    </uib-tab> 
</uib-tabset> 
+0

這......像一個魅力一樣工作,謝謝! – RandomUs1r

+0

@ RandomUs1r很高興爲您提供幫助,謝謝:) –

+0

我可能說得太快了,似乎以這種方式嵌入鏈接不會觸發活動事件,所以鏈接是可點擊的,但它不會循環活動所以我可以更新我的造型,任何想法? – RandomUs1r

0

這個答案是非常相似@Pankaj Parkar

<div ng-app="demoApp" ng-controller="mainController"> 
<div ui-view></div> 

<script type="text/ng-template" id="tabs.html"> 
    <uib-tabset active="active"> 
     <uib-tab index="$index" ng-repeat="tab in tabs" ng-click="go(tab)"> 
      <uib-tab-heading> <a ui-sref="{{tab.name}}">{{tab.name}}</a> </uib-tab-heading> 
      <div ui-view></div> 
     </uib-tab> 
    </uib-tabset> 
</script> 

angular.module('demoApp', ['ui.router', 'ui.bootstrap', 'ui.router.stateHelper']) 
.run(['$state', '$rootScope', 'TABS_CONFIG', function($state, $rootScope, TABS_CONFIG) { 
    // run needed to set the correct tab-index on load 
    var tabs = TABS_CONFIG.children; 
    $rootScope.$on('$stateChangeStart', function(e, toState, toParams, fromState, fromParams) { 

     console.log ('$stateChangeStart') 
     angular.forEach(tabs, function(item, index) { 
      if (item.name == toState.name) { 
       $rootScope.active = index; 
        console.log ($rootScope.active) 

      } 

       console.log (index) 
     }); 
    }); 
}]) 
.constant('TABS_CONFIG', { 
    name: 'tabs', 
    templateUrl: 'tabs.html', 
    abstract: true, 
    children: [{ 
      url: '/about', 
      name: 'about', 
      template: '<div class="container"><h1>about</h1></div>' 
       //templateUrl: 'about.html' 
     }, { 
      url: '/contact', 
      name: 'contact', 
      template: '<div class="container"><h1>contact</h1></div>' 
       //templateUrl: 'contact.html' 
     }, { 
      url: '/knowhow', 
      name: 'know-how', 
      template: '<div class="container"><h1>knowhow</h1></div>' 
       //templateUrl: 'knowhow.html' 
     }, 

    ] 
}) 
.controller('mainController', function($scope, $state, TABS_CONFIG) { 
    $scope.tabs = TABS_CONFIG.children; 

    $scope.go = function(tab) { 
    console.log ('go') 
     //$scope.active = $scope.tabs.indexOf(tab); 
     $state.go(tab.name); 
    }; 
}) 
.config(routes); 

function routes($urlRouterProvider, stateHelperProvider, TABS_CONFIG) { 
$urlRouterProvider.otherwise('/contact'); 

stateHelperProvider.state(TABS_CONFIG, { 
    keepOriginalNames: true 
}); 
}