Shane Stillwell
Shane Stillwell

Reputation: 3248

How do I show a block of HTML only on the index page with AngularJS

I have a block of HTML that I only want to appear on the home page. How do I tell the view that this is the home page?

Here are some of my routes

myapp.config(['$routeProvider', function($routeProvider) {
  $routeProvider
    .when('/', {
        templateUrl: 'partials/index.html', 
        controller: App.Controllers.Index
    });

How can I use ng-show to figure out if I'm on the home page or not?

Upvotes: 1

Views: 77

Answers (1)

Mark Rajcok
Mark Rajcok

Reputation: 364697

Assuming that "block of HTML" has its own view (and its own controller), you can listen for $routeChangeSuccess events, and update a $scope property that ng-show is bound to:

var BlockCtrl = function($scope) {
    $scope.showBlock = false;
    $scope.$on('$routeChangeSuccess', function(evt, cur, prev) {
       if(...determine when you want to show the block...) {
           $scope.showBlock = true;
       } else {
           $scope.showBlock = false;
       }
    }
}

See also https://stackoverflow.com/a/11910904/215945

Upvotes: 3

Related Questions