0

Is it possible to check state in the view and don't output the link for current (active) state?

Currently trying:

<a ng-if="!$state.includes('dashboard.common')" ui-sref="dashboard.common" >Dashboard</a>
<span ng-if="$state.includes('dashboard.common')">Dashboard</span>

Of course, I could decorate it with ui-sref-active, but I don't want to have link at all.

Any ideas?

9
  • What's wrong with the ng-if approach? Commented Jul 27, 2015 at 13:38
  • it seems like $state.includes doesn't work Commented Jul 27, 2015 at 13:38
  • 2
    Did you expose $state on the scope - i.e. $scope.$state = $state? Commented Jul 27, 2015 at 13:59
  • good point, thank you Commented Jul 27, 2015 at 14:10
  • 1
    Create a custom directive similar to ui-sref-active that applies the equivalent of ng-if - ui.router doesn't have such directive built-in Commented Jul 27, 2015 at 14:34

1 Answer 1

0

the answer was provided here

and my final version is:

angular.module('ui')
  .directive('uiLink', function($state) {
    'use strict';

    return {
      restrict: 'E',
      transclude: true,
      template: [
        '<a ui-sref="{{uiSref}}" ng-if="!isCurrent()" ng-transclude></a>',
        '<span ng-if="isCurrent()" ng-transclude></span>'
      ].join(''),
      link: function(scope, element, attrs) {
        scope.uiSref = attrs.sref;
        scope.isCurrent = function() {
          return $state.includes(attrs.sref);
        };
      }
    };
  });

so now you can use this directive like:

<ui-link sref="dashboard.common"><span translate="MY_DASHBOARD">Dashboard</span></ui-link>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.