I have my state configurations defined as below:
$stateProvider
.state('parentState', {
abstract: true,
url: '/:tenantId/',
param: {
tenantId: {
array: false
}
},
views: {
'header#': {
templateUrl: 'home/header.html',
controller: 'Header as vm'
},
'footer#': {
templateUrl: 'home/footer.html',
controller: 'FooterCtrl as vm'
}
},
resolve: userResolve,
data: {
private: true
}
})
//...4-5 other child states, then a state to handle unknown urls
.state('parentState.otherwise', {
views: {
'#': {
templateUrl: 'home/404/pageNotFound.html',
controller: 'PageNotFoundCtrl as vm'
}
}
});
$urlRouterProvider.otherwise(function ($injector) {
$injector.get('$state').go('parentState.otherwise', {}, {
location: false
});
});
Now, when an invalid URL is entered, parentState.otherwise state loads correctly, and parentState param, i.e. tenantId, is also correctly filled.
However, on page reload(refresh, Ctrl + R) with same invalid URL, parentState.otherwise state loads, but the problem is parentState param, i.e. tenantId is coming as empty string("").
Somehow, with location: false, the parent state, i.e. parentState in this case, was not picking up tenantId param from the URL on page refresh. So, if we explicitly pass tenantId param to child state, i.e. parentState.otherwise while opening it, everything works perfectly even on page refresh:
$urlRouterProvider.otherwise(function ($injector) {
$injector.get('$state').go('parentState.otherwise', {
tenantId: 'someTenantId'
}, {
location: false
});
});
Related
I am on angular 1.6. Trying to override value of a ui-router state.
Initial states as follows :
$stateProvider.state('parent', {
abstract: true,
parent: 'ancestor',
url: '/parent',
data: {
authorities: [A,B]
},
views: {
'content#': {
templateUrl: 'app/scripts/myapp/module/parent.html'
}
}
})
.state('child', {
parent: 'parent',
abstract: true,
views: {
'tabs': {
templateUrl: 'app/scripts/myapp/module/submodule/child.html',
controller: 'ChildController',
controllerAs: 'childController'
}
}
})
.state('grandChild', {
parent: 'child',
url: '/{dataSource:(?:option1|option2|option3)}',
views: {
'body': {
templateUrl: 'app/scripts/myapp/module/submodule/grandchild.html',
controller: 'GrandChildController',
controllerAs: 'grandChildController'
},
},
resolve: {
groups: ['MyService', '$transition$', function (MyService, $transition$) {
var selectedSource = $transition$.params().dataSource ? $transition$.params().dataSource : 'option1';
return MyService.getGroups(selectedSource);
}]
}
})
Then in one of sub-implementation I am trying to override value of url - adding option4 for one of the state.
var states = $stateRegistry.deregister('grandChild');
$stateRegistry.register(states[0]);//There is one more child. For simplicity not showing here.
states[1].url.pattern = '/{dataSource:(?:option1|option2|option3|option4)}';
$stateRegistry.register(states[1]);
when I do following on 'grandChild'
$state.go('grandChild', {dataSource: 'option4'});
I get error as
Param values not valid for state 'grandChild'. Invalid params: [ dataSource ]
when I inspect $state
I see something weird.
$state.$current.params.datasource.type.pattern = /(?:option1|option2|option3)/
$state.$current.url.pattern = "/{dataSource:(?:option1|option2|option3|option4)}"
$state.$current.url._compiled[0] = /((?:option1|option2|option3))/
I am trying to pass data from one controller to another using $stateParams but getting null instead of valid data:
Please find my route code below:
.state("root.home", {
url: "/home",
abstract: true,
template: homeHtml,
controller: "HomeCtrl as ctrl",
})
.state("root.home.content", {
url: "",
params: {
userId: null,
},
views: homeView,
})
Controller A code:
$state.go("root.home.content", {userId}); // getting correct UserId
HomeCtrl code:
// In its constructure I injected the $stateParams
init() {
console.log($stateParams); // getting undefined here
if ($stateParams && $stateParams.userId) {
this.loadUser($stateParams.userId);
}
}
Pass your parameter as below
$state.go("root.home.content", {'userId': userId});
//This userId should be a valid variable inside your Controller A
And also you should edit your router as below,
.state("root.home.content", {
url: "/:userId",
params: {
userId: null,
},
views: homeView,
})
Otherwise it will not show in the url & cannot access from $stateParams
I fixed this by adding params to parent root instead of child. The correct code is:
.state("root.home", {
url: "/home",
params: {
userId: null,
},
abstract: true,
template: homeHtml,
controller: "HomeCtrl as ctrl",
})
.state("root.home.content", {
url: "",
views: homeView,
})
controllerA from state A
$state.go('account.accountRegister', {accountType: $stateParams.accountType, acName: acName})
State defined in app.js
.state('account.accountRegister', {
url: '/:accountType/register',
params: {accountType, acName},
views: {
'mainView#':{
templateUrl: 'app/views/registration.html',
controller: 'registrationController'
}
}
})
controllerB from state B
console.log($stateParams.acName); // is getting undefined
How to use acName in the controller without showing in the url part of the state?
I came up with a solution
.state('account.accountRegister', {
url: '/:accountType/register',
params: {
acName: {
value: 'defaultValue',
squash: false
}
},
views: {
'mainView#':{
templateUrl: 'app/views/registration.html',
controller: 'registrationController'
}
}
})
For more info on squash property of the params object:
https://ui-router.github.io/docs/0.3.1/#/api/ui.router.state.$stateProvider
I have states that look like this:
$stateProvider.state('base', {
url: '/base',
redirectTo: 'base.all',
template: '<div ui-view></div>'
})
.state('base.all', {
parent: 'base',
url: '?someQuery&another',
params: {
someQuery: null,
another: null
},
views: {
'': {
template: '<div>base.all</div>',
controller: function($stateParams){
console.log($stateParams.someQuery);//undefined
console.log($stateParams.another);//undefined
}
}
//some views
}
})
.state('base.all2', {
parent: 'base',
url: '/subPath?someQuery&another',
params: {
someQuery: null,
another: null
},
views: {
'': {
template: '<div>base.all2</div>',
controller: function($stateParams){
console.log($stateParams.someQuery);//foo
console.log($stateParams.another);//bar
}
}
//some views
}
});
I listen to the event $stateChangeStart and do $state.go when redirectTo is present.
My problem now is if I go to the url /base?someQuery=foo&another=bar
It will correctly go to the base.all-state but the $stateParams of the base.all-state will be empty. I don't want this state to have a subPath. The child state will only get the urlParameters set to $stateParams if it has its own subPath like /base/subPath?someQuery=foo&another=bar then it works fine and everything is set to $stateParams.
Is my attempt to have a subState with no subUrl, but only the queryParameters, possible? If so how?
It was as simple to not use the redirectTo and set parent state to abstract.
I think I was too blinded with the use of redirectTo.
And I'm not getting any warnings when routing /base, it seems it will automatically find the state base.all and everything works as expected.
I want to be able to pass some state params that will show up in the route, and some that won't.
It would be nice to be able to do this:
.state('widgetadmin.widgets.widget', {
url: '/widget/{widgetId}/{friendlyName}',
views: {
"sub#widgetadmin": {
controller: 'WidgetAdminController as vm',
templateUrl: 'widgetadmin/widgets/widget.tpl.html',
params: { widgetHref: null }
}
},
data:{ pageTitle: 'Edit Widget' }
}
Then pass it like this:
$state.go('widgetadmin.widgets.widget',
{widgetId: widget.id, friendlyName: slug, widgetHref: widget._links.self.href},
{reload: true});
But when I try to access it, it's undefined:
var something = $stateParams.widgetHref;
How can I pass some state params in route, and some not?
Needed to put the params in a different place:
.state('widgetadmin.widgets.widget', {
url: '/widget/{widgetId}/{friendlyName}',
views: {
"sub#widgetadmin": {
controller: 'WidgetAdminController as vm',
templateUrl: 'widgetadmin/widgets/widget.tpl.html'
}
},
data:{ pageTitle: 'Edit Widget' },
params: { widgetHref: null }
}