Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can access $stateParams in "view" but not in the controller

I'm going through the AngularUI Router sample app in http://angular-ui.github.io/ui-router/sample/#/

I think I understand in general what is going on and have been able to implement something similar for my app.

My question is that it seems that you can access $stateParams from "View" files (e.g. https://github.com/angular-ui/ui-router/blob/master/sample/contacts.html - where it refers to $stateParam), but when I access the $stateParams in the controller, it returns an empty object. (https://github.com/angular-ui/ui-router/blob/master/sample/states.js#L83)

So I guess what I'm trying to do is access the state of the child view in the parent view controller via $stateParams, but it's an empty object.

Instead, I can get the state by doing $state.params.PARAMETER_NAME.

Why is that?

like image 249
ericbae Avatar asked Nov 11 '22 14:11

ericbae


1 Answers

This is by design, see https://github.com/angular-ui/ui-router/wiki/URL-Routing#two-important-stateparams-gotchas

It seems like a good idea to me, it's not very clear code if you're accessing state params from a child view in one of its ancestors.

like image 157
Thomas Parslow Avatar answered Nov 15 '22 02:11

Thomas Parslow