I have configured the following ui-router.
app.config(['$stateProvider', function ($stateProvider) {
$stateProvider
.state('global.editor', {
url: '/posts/editor/{id}',
templateUrl: '/htmls/editor.html',
controller: 'EditorCtrl',
resolve: {
post: ['$stateParams', 'codeService', function ($stateParams, codeService) {
return codeService.getPost($stateParams.id)
}]
}
}
.state('global.new', {
url: '/new',
templateUrl: '/htmls/editor.html',
controller: 'EditorCtrl'
})
.state('global.newTRUE', {
url: '/newTRUE',
templateUrl: '/htmls/editor.html',
controller: 'EditorCtrl'
})
.state('global.editor.panels', {
controller: 'PanelsCtrl',
params: { layout: 'horizontal' },
templateUrl: function (params) { return "/htmls/" + params.layout + '.html' }
}
}])
app.controller('EditorCtrl', ['$scope', '$state', function ($scope, $state) {
$scope.layout = "horizontal";
$scope.$watch('layout', function () {
$state.go('global.editor.panels', { layout: $scope.layout });
});
}]);
As a result, https://localhost:3000/#/new in a browser leads to (the state global.editor, then to) the state global.editor.panels.
Now, I want to add a parameter connected:
- I don't want it to be shown in the url
- https://localhost:3000/#/new in a browser makes
connectedto befalse, and https://localhost:3000/#/newTRUE in a browser makesconnectedto betrue connectedcan be past into the controllerEditorCtrlandPanelsCtrlconnectedcan be available in theresolveofglobal.editor; ideally, we could resolve different objects according to the value ofconnected.
Does anyone know how to accomplish this?
You can surely use the
paramsof UI-Router states' config to not show it in URL and achieve all mentioned points.Also, as per #2, you need
connectedto befalsefor/newandtruefor/newTRUE. We can do so by passingtrueorfalseas default value for those states. Something like this:For #3, In order to access
connectedin your controllers (EditorCtrlandPanelsCtrl) you can inject$stateParamsto controller and use$stateParams.connectedto get it.For #4, (This is more or less similar to achieveing #3)
Just like you get
$stateParams.id, you can have$stateParams.connectedas well, which you can use to resolve different objects according to the value ofconnected. Something like this:But, for that to work, make sure that you are passing
connectedas params when you visitglobal.editorstate (using$state.goorui-sref)Hope this helps!