2016-03-04 157 views
0

我试图根据用户当前视图隐藏/显示div,但我遇到了一些问题。这是我的控制器:基于视图隐藏/显示元素

angular.module('myApp') 
    .controller('RouteCtrl', function ($scope, $location) { 

    $scope.currentPath = $location.path(); 
    console.log($location.path()); 

}); 

在我的index.html我有这样的:

<span ng-controller="RouteCtrl"> 
    <div ng-view=""></div> 
    <div ng-include="'views/siderail.html'" ng-hide="currentPath === '/live'" ></div> 
</span> 

在我$routeProvider我有这样的:

.when('/live', { 
    templateUrl: 'views/live.html', 
}) 

出于某种原因,这并未” t似乎工作。我希望div在网站处于/ live路线时被隐藏,但似乎没有办法。如果我最初在负载上访问/ live,它会起作用,但一旦路由发生变化,它就不会重新显示div,反之亦然。几乎就像它检查一次,而不是像我想要的那样改变每一个路线。

回答

0

你需要观察的位置变化,因为加载器,当你scope变量只被定义一次。

下面的代码添加到您的控制器:

$scope.$on('$locationChangeSuccess', function() { 
    $scope.currentPath = $location.path(); 
}); 

这将改变你的范围变数每次位置变化。

0

包括在你的路线控制器:

.when('/live', { 
    controller: 'RouteCtrl', 
    templateUrl: 'views/live.html', 
}) 
+0

这仍然没有做到这一点。它在我在页面加载时访问/ live路线时有效,但是如果我返回到主路线,它根本不会显示div。它检查初始负载,但不是每次查看都改变。那有意义吗?我会更新这个问题。 –

+0

尝试在您的页面上显示“{{currentPath}}”,并在您更改页面时查看它所说的内容。 –