2015-05-26 36 views
0

我正在努力使我的類別和供應商名稱在麪包屑上可見。 我正在使用ng-breadcrumbs模塊(https://github.com/ianwalter/ng-breadcrumbs)。在麪包屑中顯示當前類別名稱(使用angularJS)

我想我有curCategory和curVendor全球無法訪問的問題,但嘗試了一些方法,無法使其工作。

這裏是我的html:

<body ng-controller="mainController as main"> 
(...) 
<ol class="breadcrumb"> 
<li ng-repeat="breadcrumb in breadcrumbs.get({'Category':curCategory,'Vendor': curVendor}) track by breadcrumb.path" ng-class="{ active: $last }"> 
    <a ng-if="!$last" ng-href="#{{ breadcrumb.path }}" ng-bind="breadcrumb.label" class="margin-right-xs"></a> 
    <span ng-if="$last" ng-bind="breadcrumb.label"></span> 
</li> 
</ol> 

我的路線:

app.config(['$routeProvider', 
    function($routeProvider) { 
    $routeProvider. 
     when('/', { 
     templateUrl: 'templates/pages/main.html', 
     label: 'Home' 
     }). 
     when('/:cat', { 
     templateUrl: 'templates/pages/category.html', 
     label: 'Category' 
     }). 
     when('/:cat/:ven', { 
     templateUrl: 'templates/pages/vendor.html', 
     label: 'Vendor' 
     }). 
     otherwise({ 
     redirectTo: '/' 
     }); 
    }]); 

我的控制器:

var app = angular.module("myApp", [ 
    'ngRoute', 
    'ng-breadcrumbs' 
]); 

app.controller('mainController', function($scope, $routeParams, breadcrumbs) { 

    $scope.breadcrumbs = breadcrumbs; 

    $scope.curCategory = $routeParams.cat; 
    $scope.curVendor = $routeParams.ven; 

    (...) 

}); 
+0

這看起來很行之有效......你能重現你的bug嗎? – Okazari

回答

0

UFF我發現什麼是錯的!

讓我感到驚訝的是,它在特定的控制器中工作正常,但是當我將它移動到mainController時,它從$ routeParams返回空對象。

這裏是解決方案:

$scope.$on('$routeChangeSuccess', function() { 
    $scope.curCategory = $routeParams.cat; 
    $scope.curVendor = $routeParams.ven; 
}); 

它$ routeParams之前剛分配變量填充。