ericbae
ericbae

Reputation: 9644

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?

Upvotes: 5

Views: 11637

Answers (2)

d3l33t
d3l33t

Reputation: 890

You could always set the params, in the parent scope

$scope.setParams = function(params) {
    $scope.params = params
}

In child controller

$scope.ParentController.setChildViewParams($stateParam);

Upvotes: 0

Thomas Parslow
Thomas Parslow

Reputation: 6012

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.

Upvotes: 1

Related Questions