Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
angularJS:如何在ng视图之外有条件地显示元素?_Angularjs - Fatal编程技术网

angularJS:如何在ng视图之外有条件地显示元素?

angularJS:如何在ng视图之外有条件地显示元素?,angularjs,Angularjs,考虑以下angular 1.X index.html文件: <div ng-app="app" class="hero-unit"> <a ng-show="!isHomePage" href="#!/home">Home</a> <div class="container-fluid"> <div ng-view></div> </div> </div> 我

考虑以下angular 1.X index.html文件:

<div ng-app="app" class="hero-unit">
    <a ng-show="!isHomePage" href="#!/home">Home</a>
    <div class="container-fluid">
        <div ng-view></div>
    </div>
</div>
我想只在位置不是主页时显示元素? 我应该如何进行?我应该将此元素映射到专用控制器上作为mainController吗?这是一个好做法吗?

尝试将ng应用程序移动到body级别,然后使用$rootScope中的服务来检测它是否在主页上-我在一些应用程序中这样做

e、 g

然后在app.js run方法中,将NavigationService设置为rootScope

e、 g

$rootScope.NavigationService=NavigationService

您可以检查当前url: 请参见以下示例:

实现这一目标的途径:

您可以在应用程序中创建父级控制器外侧ng视图,然后检查当前路由并基于该视图实现条件

家 要获取当前路径,可以使用:

app.run(function ($rootScope) {
    $rootScope.$on('$routeChangeSuccess', function (e, current, pre) {
        console.log(current.originalPath); // Do not use $$route here it is private
    });
});

$location.path

如果您想根据视图中的任何事件执行此操作,可以使用$scope.$emit。$emit将有助于子级与父级之间的通信。 您可以创建一个角度服务,通过它您可以传递应用程序的当前状态,并基于该状态实现条件。
在您提供的侦听器中,我添加了`$rootScope.showButton=current.originalPath.indexOfhome!==-1.假:真;`但是在父级控制器div中,{{showButton}}为空。你能给出一个html和js的完整例子吗?什么是current.originalPath?我发现,这是一个模板引擎冲突!旧的应用程序原始模板引擎试图计算花括号之间的角度表达式!谢谢你的回答!
 <div ng-controller="MainCtrl as main">
    <div class="view-animate-container">
      <div ng-view class="view-animate"></div>
   </div>
    <hr />

  $location.path() = {{main.$location.path()}}
  $route.current.templateUrl = {{main.$route.current.templateUrl}}
  $route.current.params = {{main.$route.current.params}}
  $routeParams = {{main.$routeParams}}
 </div>
app.run(function ($rootScope) {
    $rootScope.$on('$routeChangeSuccess', function (e, current, pre) {
        console.log(current.originalPath); // Do not use $$route here it is private
    });
});