2016-09-29 57 views

回答

0

當你定義的路由/狀態(取決於您是否使用ngRouter/uiRouter),只需添加一個麪包屑對象,以它像這樣:

$stateProvider 
    .state('courses', { 
     url: '/create', 
     template: ...., 
     controller: ...., 
     breadcrumbs: [ 
     { 
      name: Home, 
      url: '/home' 
     }, 
     { 
      name: Create User, 
      url: '/create' 
     } 
     ] 
    }) 

而在你的控制器,注入breadcrumbs作爲依賴,和ng-repeat了他們對你的模板

//Controller 
.controller('mycontroller', function mycontroller($scope, breadcrumbs) { 
    $scope.breadcrumbs = breadcrumbs; 
} 



//Template 
<span class="breadcrumbs"> 
    <a ng-repeat="breadcrumb in breadcrumbs" ng-bind="::breadcrumb.name" href="{{breadcrumb.url}}"></a> 
</span> 
+0

感謝您迴應,將它的工作當我的父母狀態改變時,是否有可能獲得動態狀態,因爲我不知道哪個狀態會在我的應用中下一個 –

+0

我有一個主模塊和幾個子模塊les和在每個子模塊中,我有他們每個人的狀態 –

+0

甚至更​​多我不確定以前的狀態,哪一個將是我以前的狀態 –

0

我已經使用狀態改變的成功來獲得當前和以前的狀態 和我需要的是工作一樣。

以下是我在主模塊JS做了(在我的情況app.js):

angular.module('app').run(['$rootScope', function ($rootScope) { 
    $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams, error) { 
//goes here your code to manage states and their respective labels 
      $rootScope.breadcrumbStates = []; 
      $rootScope.breadcrumbLabels = []; 
      $rootScope.breadcrumbStates.push(fromState.name); 
      $rootScope.breadcrumbStates.push(toState.name); 
      $rootScope.breadcrumbLabels.push(fromState.label); 
      $rootScope.breadcrumbLabels.push(toState.label); 
}]); 

HTML

<ul class="breadcrumb"> 
     <li ng-repeat="breadcrumb in breadcrumbStates track by $index">    
       <a ui-sref="{{breadcrumb}}">{{breadcrumbLabels[$index]}}</a> 
     </li> 
    </ul>