how can I use ng-if based on url in the index.html page?

5.3k Views Asked by At

I want to use ng-hide in the index page if the url in some nested views. in my index.html, I want something like:

    <top-bar ng-if="!home"></top-bar>
    <ui-view class="reveal-animation"></ui-view>
    <bottom-bar ng-if="!home"></bottom-bar>

I want that when I enter to "home" view the bars will disappeared. as I saw here in same questions - the answer was to use $location in the controller, but where is the controller of the index page?

thanks

2

There are 2 best solutions below

8
On BEST ANSWER

In a parent controller, add the following:

$scope.isHome = function(){
  return $state.is("home");
}

Then change your template like this:

<top-bar ng-if="!isHome()"></top-bar>
<ui-view class="reveal-animation"></ui-view>
<bottom-bar ng-if="!isHome()"></bottom-bar>

Check this plunkr here to see some live code.


Another method would be using $stateChangeSuccess, something like this:

$scope.$on("$stateChangeSuccess", function(event, toState){
  $scope.isHome = (toState.name == "home")
})

I also recommend checking out $state.includes, $state.current and others. Just get through the documentation here

1
On

It is not easy to teach you everything in one go. You have to refer to few concept.

http://viralpatel.net/blogs/angularjs-routing-and-views-tutorial-with-example/

I hope this link will help you to getting started with Angularjs.