204

HTML ソース コード

<div ng-app="">
    <div ng-controller="test">
      <div ng-address-bar browser="html5"></div>
      <br><br>
      $location.url() = {{$location.url()}}<br>
      $location.search() = {{$location.search('keyword')}}<br>
      $location.hash() = {{$location.hash()}}<br>     
      keyword valus is={{loc}} and ={{loc1}}
  </div>
</div>

AngularJS ソースコード

<script>
function test($scope, $location) {
  $scope.$location = $location;
  $scope.ur = $scope.$location.url('www.html.com/x.html?keyword=test#/x/u');
  $scope.loc1 = $scope.$location.search().keyword ;    
    if($location.url().indexOf('keyword') > -1){    
        $scope.loc= $location.url().split('=')[1];
        $scope.loc = $scope.loc.split("#")[0]        
    }
  }
 </script>

ここで、変数locloc1両方が上記の URL の結果としてtestを返します。これは正しい方法ですか?

4

8 に答える 8

164

$locationルーティングは確かにアプリケーション レベルの URL 解析の優れたソリューションですが、独自のサービスまたはコントローラーに挿入された、より低レベルのサービスを使用することもできます。

var paramValue = $location.search().myParam; 

この単純な構文は、http://example.com/path?myParam=paramValue. $locationProviderただし、以前に HTML 5 モードでを構成した場合のみ:

$locationProvider.html5Mode(true);

それ以外の場合は、もう少し複雑なhttp://example.com/#!/path?myParam=someValue "Hashbang" 構文をご覧ください。良い。

于 2013-10-20T19:33:02.070 に答える
32

ngRoute を使用している場合は$routeParams、コントローラーに注入できます

http://docs.angularjs.org/api/ngRoute/service/$routeParams

angular-ui-router を使用している場合は、注入できます$stateParams

https://github.com/angular-ui/ui-router/wiki/URL-Routing

于 2014-03-24T01:20:39.210 に答える
2

すでに投稿された回答が役に立たなかった場合は、$location.search().myParam; を試すことができます。URL http://example.domain#?myParam=paramValue

于 2016-02-26T00:11:21.190 に答える