-
Notifications
You must be signed in to change notification settings - Fork 135
Query Params and Route matching #176
Comments
Doesn't seem supported yet. But I think it's essential that query params are either put into $routeParams or that $location.search() changes triggers view updates. Right now I have to do this: home.js function HomeController($routeParams, $location, $scope){
var self = this;
this.params = $routeParams;
// set query when coming to this route anew
refreshQuery();
function refreshQuery(){
self.query = $location.search();
}
// set query when only the $location.search value changes, also deregister on destroy
$scope.$on("$destroy", $scope.$on("$locationChangeSuccess",refreshQuery));
} home.html <h2>Home</h2>
<p>{{ home.params | json }}</p>
<p>{{ home.query | json }}</p> In index.html <a href="#/home">Go Home</a>
<a href="#/home?foo=5&bar=10">Go Home with Query</a>
<a href="#/new">Go New</a> When you go from '/home' to '/home?foo=5...' the controller is not reloaded, so that's why I need to listen for $locationChangeSuccess. If we don't have built in support, everyone is going to have to add some custom code like this and that seems unnecessary. |
Route recognizer is definitely solid in its support for query params. https://github.com/tildeio/route-recognizer/blob/master/tests/recognizer-tests.js Hopefully that means adding support will be easier. |
I had a look into this and actually it is possible and it isn't possible at the same time. With the ng-link directive, you can specify your query string parameters. <a ng-link="home">Go Home</a>
<a ng-link="home({queryParams: {foo: 5, bar:10}})">Go Home With Query</a> The link is parsed by Route Recognizer and they use the But if you do this, it will result in your html as I'll add a pull request for this issue. Not sure if it will be accepted. Maybe there is a reason why they want it to be with |
I've been thinking about this as well, but I don't feel like I have an especially strong feeling of the best way to solve it. I'm totally open to suggestions though. |
Tried to fix it in <a ng-link="home({queryParams: {foo: 5, bar:10}})">Go Home With Query</a> But when I wrote tests for this, calling This is what I wrote as a test it('should work with querystrings', inject(function ($router) {
$router.config([
{ path: '/user/:name', component: 'userAge' }
]);
compile('<ng-viewport></ng-viewport>');
$router.navigate('/user/brian?age=10');
$rootScope.$digest();
expect(elt.text()).toBe('hello brian (10)');
$router.navigate('/user/igor');
$rootScope.$digest();
expect(elt.text()).toBe('hello igor (unknown)');
})); This is the result of running that test
So although I pass the querystring to the controller when I use Brian, the controller doesn't receive it. But when navigating away to igor (with no queryparams), then the params are being received by the controller. Didn't have time to look more into this behaviour. |
@btford I think the ui-router had a simple enough way of dealing with query parameters https://github.com/angular-ui/ui-router/wiki/URL-Routing. It allows us to specify query parameters directly in the URL path.
To interact with the parameters then I could utilize $routeParams to grab the parameter as:
If we want to pass query parameters I like the simplicity of ui-router also. It doesn't make me specify queryParams like @SamVerschueren pointed out. Instead, it is smart enough to know if they are query parameters or URL parameters:
& programmatically:
|
The new router uses Route Recognizer for this so I guess we should follow their rules... It is that library that forces you to add the queryParams property. |
I don't like that you have to specify query params:
@btford Why can't we just pass the object as we do in ui router. I get that this change will make it easier to build up query params as you don't have to look at the route to determine which properties are actually part of the route vs query parameters? |
@niemyjski good on you for writing up such a detailed migration experience!! 👏 👍 That will be incredibly valuable to @btford and the rest of the new router team! I also agree that I'd rather just pass query params along with the other params and not on a special queryParams object, if possible. |
@niemyjski My fix works for the ng-link directive. But when I tried to create tests for that functionality by calling Passing query params directly instead of via the |
Is there a way for us to specify in our router.config query parameters?
I would like to hit something like the following URL
If I map my router like the following my information controller get's triggered, but the $routeParams only has {id: 42, type: 'wisdom'}. No isUser property is in the object.
If I map my router like the following my information controller get's triggered, but the $routeParams has {id: "42", type?isUser: "wisdom"}. The type and isUser properties are not separated and still no query parameter
If I map my router like the following my information controller doesn't get triggered.
The text was updated successfully, but these errors were encountered: