2015-12-04 31 views
1

我的目标是在angularjs控制器上获得true如果URL为#how htttp://www.example.com#how或false如果url没有#how​​。Angularjs检查或网址上存在

webApp.controller('ctrl',function($scope){ 

$scope.showHow = ???; // if url with #how == true else false 
... 

回答

1

我使用类似这样的东西。

webApp.controller('ctrl',function($scope) { 
    $scope.showHow = window.location.hash.indexOf('how') > 0 
} 
+0

使用Angular的'$ window'服务通常更好的做法是 – Ankh

1

您可以使用$位置服务

webApp.controller('ctrl',function($scope, $location){ 

$scope.showHow = $location.hash() === 'how'; // if url with #how == true else false 

查看详情here

但在一般情况下,如果应用程序应显示URL改变一些内容,最好的办法是使用default angular's routingui router