Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Html source code

        <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>

Angular js source code

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>

Here loc and loc1 both returns test as result for the above url
Is this the correct way ?

share|improve this question
You may want to check out $routeParams. – Rosarch Nov 8 '12 at 22:37

2 Answers

You may want to use Angular's built-in routing rather than do it yourself. Here are a few tutorials to get started:

I also have a sample app on Github that implement's routing, if you wish to take a look.

share|improve this answer

I know this is an old question, but it took me some time to sort this out given the sparse Angular documentation. The RouteProvider and routeParams is the way to go. The route wires up the URL to your Controller/View and the routeParams can be passed into the controller.

Check out the Angular seed project. Within the app.js you'll find an example for the route provider. To use params simply append them:

$routeProvider.when('/view1/:param1/:param2', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'});

Then in your controller pass in $routeParams:

.controller('MyCtrl1', ['$scope','$routeParams', function($scope, $routeParams) {
  var param1 = $routeParams.param1;
  var param1 = $routeParams.param2;
  ...
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.