-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ui-sref initialze controller twice if url has params #1476
Comments
Please test this against the latest release. |
@nateabele tested on 0.2.12-pr1 with angular 1.2.26, same result. Made a demo - https://dl.dropboxusercontent.com/u/18659172/angular-ui-router-bug/index.html |
Ah okay, maybe I was wrong about it already being out in a release. @christopherthielen can you weigh in here? |
I believe this is fixed in master, but not 0.2.12-pre1 |
@christopherthielen my initial bug report was about version 0.2.11 which is current master state. I will prepare demo using 0.2.11 today |
Master needs to be built manually. |
@christopherthielen just made 2 builds with 'grunt build' (for master & master-0.2.12-pr1). Both of them has that bug. Updated demo & zip file are located on prev links. |
Thanks I'll look at this early next week |
Came across this bug as well, ui-router 0.2.11 and angularjs 1.3.2. From my observations, a controller is called twice only when the url parameter is defined but not passed in ui-sref (a.k.a optional route parameters), exactly like in @ruslansavenok's example. |
@ognus thanks that helps. The first transition via ui-sref="lessons" calls I'm wondering why we allow transitions when the required parameters are not provided. I have a TODO somewhere about aborting transitions when non-optional parameters are not provided. |
So the solution for now is to pass that argument as an empty string, works for my case :) |
Note: 0.2.12 is coming in the next few days (unless I find any other show stoppers). 0.2.12 will allow you to specify |
@christopherthielen briliiant! thank you for that. Thanks @ruslansavenok for hack suggestion, works fine for me as well and will probably stick with just passing empty string till 0.2.12 release. |
I've decided that 0.2.12 will continue to map undefined path parameters as an empty string, like it has done since at least 0.2.0. 0.3.0 will error if you try to transition without all required parameters. |
For those who stumble upon this issue after it has been closed, chances are you googled "angular ui router optional parameter controller loaded twice" or something like this... here is a recap of how it can be fixed : Let's say you have a state with an optional parameter : .state('app.numbers', {
url: '/numbers{timestamp:.*}',
templateUrl: 'numbers/templates/app.numbers.html',
controller: 'NumberListCtrl'
}) if you just use a link like this, the controller <a ui-sref=".numbers">numbers</a> To prevent your controller from being loaded twice, you can use the following methods : <a ui-sref=".numbers({timestamp:''})">numbers</a> or use $state.go('app.numbers', {timestamp: ''}); or (DIRTY 💩) add these lines at the top of your controller : // prevent double controller load because of the optional parameter
if (typeof $stateParams.timestamp === 'undefined') {
return;
} Hope this helps. |
@eswak Thanks a lot, very useful. |
Thanks for the tips, but I'm still affected by this somehow on 0.2.15. I'm using the I'm using nested states with my controller setup, don't know if that might have any impact. |
Orbitbot post a plunker showing the issue please |
I have the same issue with 0.2.15. For me, it breaks when I have 3 params in the ui-sref, regardless or router setup. html: e.g. var jobsDetail = { |
I have the same problem with 0.2.15 |
Actually, the root cause for it is NOT the count the params, but one of the param has a slash in it. It works fine after I encoded param first. |
I had the same issue, that one of the param values has a slash in it, thus the controller is invoked twice: once with encoded param and the other time without encoding. This is tested with ui router v0.2.15 today. html page: angular routing js file snippet: |
Oh ,that is it!there is a slash in my param too。 On Wed, Sep 30, 2015, 4:53 AM Madhav Ayyagari [email protected]
|
I have similar issue here: Navigating to: UPDATE I had two ui-view directives in my template. One of them was inside a ng-if="false" (after evaluating) div. Anyway that was causing my controller loading twice. |
I opened a new issue for the problem where the controller runs twice when one of the params has a slash. |
Hello, I am facing the similar issue. the controller is called twice which is causing a major problems. I am surprised the issue still exists . Can anyone help me how can i resolve the issue. The parameter passed here is mandatory and it can be Y or N. See the example. Configuration
Please help me out . I could not find any answer . |
hi, using ui-router in angularjs i have nested the route but the problem is if i have assign controller to parent and i want to define it in different js file. I mean i dont want to define controller in app.js then what is the method |
This problem still exists in ui-router 0.2.15 along with angular js 1.4.8 version. Which cause severe problem in whole application. Is there any workaround for this? |
But there is no any slash's in my application. |
+1 Still having this issue. No slash in my route param. |
If this problem persists, post a plunker to a new ticket that demonstrates the issue. Feel free to reference back to this ticket number 1476. Posting +1 is completely unactionable unless you provide a reproduction |
My apologies, @christopherthielen . I'm trying to get a plunker working but I'm having trouble replicating the issue on its own. I'll open a new ticket once I can get more debugging information. Thanks! |
@jongunter did you try the method I described earlier in the conversation ? I included code snippets that don't have slash in the route param |
@eswak I tried the method and it didn't work. After a week of debugging, I found my issue was related to having an unnamed view in the page with no templateUrl. Edit: oops sorry tagged the wrong person. |
I created a plunker to reproduce the issue. It is caused by v0.2.15 which is what I have. When I upgraded to v0.2.18 the issue seems to be resolved. |
@christopherthielen, unfortunately the bug still exists with v0.2.18. Even with plunker shown above. Here's my config:
Funny thing is that "document.resources" creates controller once, but any further controllers are being created twice. |
@76200 post a plunker if you can recreate the issue. |
@christopherthielen, the one created by @anilvangari works as well: https://plnkr.co/edit/4ibkGI49r2k0GBWjWKFe?p=preview |
@76200 if you switch that plunker to 0.2.18 it works: |
Yep, right. Sorry. I'll try to reproduce it later |
Ok @christopherthielen I've reproduced it. In my application I have to create some states at runtime. |
@76200 thanks for the plunker that plainly demonstrates the issue. You've defined multiple states with the same url pattern. In general, this is a questionable practice. For example, if the user goes to However, I do agree that after transitioning to a target state via ui-sref, then transitioning to the other state that also matches the URL is surprising behavior. Since that's a separate issue (with somewhat similar symptoms), can you open a separate ticket and add your plunker? We may be able to add a check when syncing the URL that avoids a new transition if the URL matches the current state. That would allow you to keep your ambiguous urls On a side note, you're also doing some weird things in your state defs like specifying |
Hey guys, I am using How to fix this actually? |
Keep in mind that if you have a controller referenced both ui-route state and in the HTML file it will initialized twice. Alternatively, the
Remove extra ng-controller from contacts.html
This may help solve a few issues around this topic. |
Having a similar issue where
causes a clusterbomb of page-load duplicate errors, but
does not. Versions 0.3.2 and 0.4.1. I am doing something obviously wrong here? |
@sachabest shouldn't be problematic. Can you put the code in a plunker to reproduce the problem? |
Hey guys, I'm having an issue related to this one, my resolve state is called twice. The thing is I am moving my ID based urls to a SEO friendly strings urls. I already changed my API to be able to response if I query with an ID or a slug name. But my router is getting the response using the slug name (this-is-an-example) but then the state is called again using the ID of the (this-is-an-example) document and overwriting the url with the ID instead of the slug. (Even refreshing the page and typing manually the slugName into the URL)
Using 2.18 router version and angular 1.4.12 |
Same here. Very small demo:
I've noticed some lag time in one of my larger applications, so I have been building AngularJS apps from the ground up and noticed that even in such a small application, the controller and its scoped functions are called twice (please assume that the HTML and angular.js and angular-ui-router.js files are functioning as normal). When I open the console, the line 'product is loaded' is called twice. I have no idea what's going on. Not a deal breaker, but for a large application, lag will be noticeable. Any reason for this? Seems like a non-invasive block of code for controllers to be called twice. |
it's not normal. Please create a plunker. |
I'm having the same problem running 0.2.18 foo : abstract During this transition back the "onExit" function is called on the abstract view "foo" http://plnkr.co/edit/tGzHwGJmD7TIqJYyIaFm?p=preview Edit: updating to 1.0.3 solved the double loading problem... but it's seems to still be calling the "onExit" but now it calls the "onExit" and then the "resolve" so it's not so much of a problem |
Using ui-router v0.2.11
My Html
My coffee
Opening lessons state, and then going to flash_lessons using ui-sref="flash_lessons" will initialize controller twice.
The text was updated successfully, but these errors were encountered: