如何在角度ng视图中隐藏index.html页面的某些内容?

Ken*_*yan 2 angularjs angularjs-directive ng-hide ng-view

当我访问angularjs中的视图时,我想知道是否可以隐藏index.html文件中的某些部分.

我的index.html文件包含所有视图的底部菜单栏,我创建了一个home.html视图,我不想包含该菜单栏.角度有可能吗?

谢谢!

Rod*_*tou 5

少$ rootScope

$ rootScope的问题在于您将为代码添加依赖项.maddygoround的答案是一个很好的答案,但你的index.html,home.html和控制器将是硬连接的,并且更改将更难做.

为了摆脱$ rootScope,我会做以下更改:

<div ng-hide="hideit">This is bottom bar<div>
Run Code Online (Sandbox Code Playgroud)

这与maddygoround发布的代码相同,它可以执行它应该执行的操作.现在,您将添加一个侦听器来更改路径,而不是到达home.html控制器并更改hideit.

下面的代码可以添加到与angular.module()声明相同的文件中;

app.run(function($rootScope, $location){
  $rootScope.$on('$routeChangeStart', function(event, next, current){
    if ($location.path() == '/home') {
      $rootScope.hideit = true;
    };
  });
});
Run Code Online (Sandbox Code Playgroud)

这将使您的index.html独立于home.html控制器中发生的事情.这是更多的代码,但可以挽救你的未来生活.我正试图摆脱$ rootScope,如果可以的话,我会在没有$ rootScope的情况下更新我的答案.