Well, I have this project, and ui-router is giving me hard times. I made a quick Plunker demo: http://plnkr.co/edit/imEErAtOdEfaMMjMXQfD?p=preview
So basically I have a main view, index.html
, into which other top-level views get injected, like this operations.index.html
. The pain in my brain starts when there are multiple named views in a top-level view, operations.detail.html
and operations.list.html
are injected into operations.index.html
, which is in turn injected into index.html
.
Basically, what I'm trying to achieve is the following behaviour:
- When a user clicks
Operations
item in the navbar, a page with empty (new) operation is shown. The URL is/operations
. - When they select an item in a list, the fields are updated with some data (the data is requested via a service, but for simplicity let's assume it's right there in the controller). The URL is
/operations/:id
. - If they decide that they want to create a new item, while editing a current one, they click
New operation
button on top of the list, the URL changes from/operations/:id
to/operations
. - No matter new or old item, the item
Operations
in the navbar stays active. - If the user is editing an item, it should be highlighted as active in the list, if they create a new item —
New operation
button should be highlighted, accordingly.
Now, check out the weird behaviour: go to Operations
and then click Operations
navbar item again. Everything disappears. The same happens if I do Operations
-> select Operation 1
-> select New operation
.
Besides, please, check out the part where I try to get the id
parameter:
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
if (toParams) {
if (toParams.id) {
for (var i = 0; i < vm.operations.length; i++) {
if (vm.operations[i].id == toParams.id) {
vm.operation = vm.operations[i];
break;
}
}
}
}
});
I am no expert, but it seems too long and complex to be true, especially for such a simple task as getting a request parameter. If I try to check on state change $stateParams
the object is empty, hence this workaround. If I try to tamper with states in app.js
, things change slightly, but there are always bugs like Operations
navbar item losing its active state or other weird stuff.
I know that asking such general questions is uncommon in SO, but I really can't grasp the concept of the ui-router, and I can feel that I'm doing things wrong here, and I would really appreciate any help in pointing me in the right direction of how to properly use ui-router for my purposes. Cheers.