Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

simple wiki

I'd like to show an edit button in the navbar is state == 'page'

v0.2.10

.state('page',{
    url: "/wiki/{ns:[0-9a-zA-Z\-/_]+}/{wp:[0-9a-zA-Z\-/_]+}",
    templateUrl: "views/wiki/view.html",
    controller: "PageController"
})

I tried

<li ng-show="$state.$current.name == 'page'"><button type="button" class="btn btn-default navbar-btn">Edit</button></li>

and

<a ng-class="{hidden: $state.$current.name == 'page'}" class="btn navbar-btn btn-primary" ui-sref="edit">Edit</a>

even

$state.includes('page')

and

$state.is('page')
share|improve this question
    
those 2 questions have absolutely nothing in common –  dalu Apr 15 at 20:53
    
Whoops, you're right. I misread and though he was referring to ngRouter. –  rossipedia Apr 15 at 20:55

1 Answer 1

up vote 1 down vote accepted

I do this all the time in projects. The trick is to add a method on scope to check states.

$scope.is = function(name){
   return $state.is(name);
}

$scope.includes = function(name){
   return $state.includes(name);
}

Html:

<li ng-show="is('page')">
<a ng-class="{hidden: is('page')}">...</a>

I generally put those on root scope, but they are extremely useful. Another thing to look into is using the ui-sref-active directives. They are very good when you are creating links to states, and also want to auto-magically add a class when that state is active.

share|improve this answer
    
Ok that would work, but how would I attach it to the navbar? Since the navbar is on the $rootScope and does not have a controller. With ngRoute I'd define a NavbarController, nevermind, I'll try that and if it doesn't work, come back to bug you ;) –  dalu Apr 15 at 21:07
    
@dalu lol, just let me know. I can help. –  Nix Apr 15 at 21:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.