Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular.js: ng-show element when $state.current.name is a certain value

I am looking to show a block of HTML only when $state.current.name equals about.list. So far I have the following code but it doesn't seem to be toggling the element depending on the state.

index.html

<nav class="global-navigation">
    <ul class="list">
    <li class="list-item">
      <a class="list-item-link" ui-sref="home">
        Home
      </a>
    </li>
    <li class="list-item">
      <a class="list-item-link" ui-sref="about">
        About
      </a>
    </li>
     <li class="list-item" ng-show="$state.current.name == 'about.list'">
      <a class="list-item-link" ui-sref="about.list">
        List
      </a>
    </li>
    </ul>
  </nav>

app.js

var myApp = angular.module('myApp', ['ui.router'])

  .config(['$urlRouterProvider', '$stateProvider', 

    function($urlRouterProvider, $stateProvider) {

    $urlRouterProvider.otherwise('/404.html');

    $stateProvider.

      // Home
      state('home', {
        url: '/',
        templateUrl: 'partials/_home.html',
        controller: 'homeCtrl'
      }).

      // About
      state('about', {
        url: '/about',
        templateUrl: 'partials/_about.html',
        controller: 'aboutCtrl'
      }).

      // About List
      state('about.list', {
        url: '/list',
        controller: 'aboutCtrl',
        templateUrl: 'partials/_about.list.html',
        views: {
          'list': { templateUrl: 'partials/_about.list.html' }
        }
      });

  }]

);
like image 663
Matt Avatar asked Mar 30 '14 19:03

Matt


People also ask

What is Ngshow in angular?

The ng-show Directive in AngluarJS is used to show or hide the specified HTML element. If the given expression in the ng-show attribute is true then the HTML element will display otherwise it hides the HTML element. It is supported by all HTML elements.

What does ng cloak do?

The ng-cloak directive prevents the document from showing unfinished AngularJS code while AngularJS is being loaded. AngularJS applications can make HTML documents flicker when the application is being loaded, showing the AngularJS code for a second, before all code are executed.

Can we use ng show and Ng hide together?

Absolutely not. First of all, the two directives can trip over each other( see this JSFiddle, as provided by Joel Skrepnek), and is generally just bad design.

How do you use NG disability?

Definition and UsageThe ng-disabled directive sets the disabled attribute of a form field (input, select, or textarea). The form field will be disabled if the expression inside the ng-disabled attribute returns true. The ng-disabled directive is necessary to be able to shift the value between true and false .


3 Answers

You can use filters like isState or includedByState.

ng-if="'about.list' | isState" // exactly 'about.list'

ng-if="'about' | includedByState" // works for about and its children about.*
like image 128
devside Avatar answered Sep 28 '22 17:09

devside


Or

JS

.run(function ($state,$rootScope) {
    $rootScope.$state = $state;
})

HTML

data-ng-show="$state.includes('about.list')"
like image 30
Whisher Avatar answered Sep 28 '22 17:09

Whisher


The view (html) doesn't know about the variable $state. It only knows the $scope that is associated with the view.

You can expose the $state variable on the $scope inside the controller that is associated with this view (you might have to inject $state into your controller as well):

$scope.uiRouterState = $state;

Then change the expression in your markup slightly:

<li class="list-item" ng-show="uiRouterState.current.name == 'about.list'">
like image 35
Sunil D. Avatar answered Sep 28 '22 17:09

Sunil D.