Denni Adam
Denni Adam

Reputation: 77

How generate url in angular ui-router and put it to location

View has controls for selecting cities and selecting params. And this view show selected objects for the selected cities. When view is loaded I parse the url and do request to the server. Url for the view are some as: http://example.com/cities?cityId=3&cityId=33&param1=value1. My questions:

  1. how can I put that url in location?
  2. how can I handle changed url in location?
  3. in which event I must hang the handler?

Upvotes: 0

Views: 382

Answers (2)

Denni Adam
Denni Adam

Reputation: 77

Url generating automatically by $state.go('cities', {/* params */}, {location: true}).

In controller I put handler in $scope.$on('$stateChangeSuccess'..

Upvotes: 1

M. Junaid Salaat
M. Junaid Salaat

Reputation: 3783

You can generate URL with

$location.url('cities?cityId=3&param1=value1');

and You can handle this in your router config $stateProvider where you define states

.state('cities', {
          url: "/cities?cityId&param1",
          templateUrl: "....",
          controller: "citiesController"
        })
// will match to url of "/cities?cityId=[any id]&param1=[any value]"

and finally You can have these parameters in the citiesController.js i.e

console.log($stateParams);
//Object {cityId: "3", param1: "value1"}

Hope it helps.

Upvotes: 2

Related Questions