2015-12-04 43 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