How to use ncy-breadcrumb in child state of abstra

2019-08-11 11:48发布

问题:

I am new to AngularJS and I used ncy-breadcrumb for my AngularJS project. There is an abstract true parent state and two child states of it. I used these child states for tabs. But I couldn't find a way to show these states in the breadcrumb dynamically. The only thing I can do is hardcode one child state name as parent in other state. But I need a solution to display these child states in collectionsWorkPage state dynamically.

.state('collectionsLibrary', {
   url: '/headquarters/collections-library/',
   templateUrl: 'app/views/collectionsLibrary/base.html',
   controller: 'CollectionsLibraryBaseController',
   ncyBreadcrumb: {
      label: 'Collections Library',
      parent: 'headquarters'
   },
   abstract: true,
   resolve: {
      controller: function ($q) {
         var deferred = $q.defer();
         require(['controllers/collectionsLibrary/CollectionsLibraryBaseController'], function () {
            deferred.resolve();
         });
         return deferred.promise;
      }
   }
   })

.state('collectionsLibrary.available', {
   url: 'available/',
   templateUrl: 'app/views/collectionsLibrary/available.html',
   controller: 'CollectionsLibraryAvailableController',
   ncyBreadcrumb: {
      label: 'Collections Library-Available',
      parent: 'headquarters'
   },
   resolve: {
      controller: function ($q) {
         var deferred = $q.defer();
         require(['controllers/collectionsLibrary/CollectionsLibraryAvailableController'], function () {
            deferred.resolve();
         });
         return deferred.promise;
      }
   }
})

.state('collectionsLibrary.my', {
   url: 'my/',
   templateUrl: 'app/views/collectionsLibrary/my.html',
   controller: 'CollectionsLibraryMyController',
   ncyBreadcrumb: {
      label: 'Collections Library-My',
      parent: 'headquarters'
   },
   resolve: {
      controller: function ($q) {
         var deferred = $q.defer();
         require(['controllers/collectionsLibrary/CollectionsLibraryMyController'], function () {
            deferred.resolve();
         });
         return deferred.promise;
      }
   }
})

.state('collectionsWorkPage', {
   url: '/headquarters/collections-library/:id/edit/',
   templateUrl: 'app/views/collectionsWorkPage.html',
   controller: 'CollectionsWorkPageController',
   ncyBreadcrumb: {
      label: 'Edit Collection',
      parent: 'collectionsLibrary.available'
   },
   params: {
      data: {}
   },
   resolve: {
      controller: function ($q, $stateParams) {
         var deferred = $q.defer($stateParams);
         require(['controllers/CollectionsWorkPageController'], function () {
            deferred.resolve();
         });
         return deferred.promise;
      }
   }
})

回答1:

app.config(['$breadcrumbProvider', configNcyBreadcrumb])

function configNcyBreadcrumb($breadcrumbProvider) {
    $breadcrumbProvider.setOptions({
        includeAbstract : true
    });
}


回答2:

The parent property can be either a string or a function returning the parent name. The function provides the scope of the current state controller (the same used for labels interpolation).

So you can do something like this:

ncyBreadcrumb: {
  label: 'Edit Collection',
  parent: function($scope) {
    if($scope.tab === 'MY') // Constant defined in CollectionsLibraryMyController
      return 'collectionsLibrary.my';
    else if($scope.tab === 'AVAILABLE') // Constant defined in CollectionsLibraryAvailableController
      return 'collectionsLibrary.available';
  }
}

See API reference for more details