Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Bug: ng-view required for routing #1580

Closed
jgallen23 opened this issue Nov 15, 2012 · 14 comments
Closed

Bug: ng-view required for routing #1580

jgallen23 opened this issue Nov 15, 2012 · 14 comments

Comments

@jgallen23
Copy link

I have an app that I would like to do some custom routing for. I'd rather not use multiple controllers and templates for my routes and I'd like to handle the routes myself. I've found that when I listen to the $routeChangeSuccess event, it will only fire if there is an ng-view on the page. Here are is the code:

Not working: http://jsfiddle.net/mxdPq/9/
Working: http://jsfiddle.net/mxdPq/10/

I'm not really sure if this is a bug or just not clear in the documentation. I don't think I should have to use an ng-view with a routeProvider, but I'm still pretty new to angular and still learning the ropes.

@jtymes
Copy link
Contributor

jtymes commented Nov 15, 2012

How will you handle routes without multiple templates?
What is it about Angular's routing that will not work for you?
Could you explain your use case a bit more?

I'm genuinely curious so I can understand how best to tackle your question. There's still a possibility that ng-view and the routing around that would work for you.

@jgallen23
Copy link
Author

So I'm re-writing an app I built called HubSearch (it's a better github search). http://projects.jga.me/hubsearch/

It's a very simple app that really only has two states (without search results and with search results). I built it with only one controller and one template, but it has two routes: / and /:language/:query. Both routes go to the same controller and both use the same template. So rather than using the automatic routing, I'm watching for the $routeChangeSuccess event and passing that on to the controller.

In that scenario, how would you structure your controllers and routes?

You can see my progress here: https://github.com/jgallen23/hubsearch/tree/feature/angular

@jtymes
Copy link
Contributor

jtymes commented Nov 15, 2012

You can have multiple routes go to a single controller. You just have to check out your $routeParams and determine what you want to do based on that.

@jgallen23
Copy link
Author

I can't seem to get that to work either: http://jsfiddle.net/3hy8P/5/

@jtymes
Copy link
Contributor

jtymes commented Nov 15, 2012

Here's a simple example
http://jsfiddle.net/HtsHe/

@jgallen23
Copy link
Author

Thanks. I was trying to get away from using a template in the route, but I guess that's not possible at this time.

I think the bug is still valid because if I want to do custom routing, the events shouldn't only fire if an ng-view exists.

@jtymes
Copy link
Contributor

jtymes commented Nov 15, 2012

Where do you want the template to be? Or do you not want templates at all?
Here's a more relevant example for you: http://jsfiddle.net/HtsHe/2/

I agree about the ng-view coupling unless a more experience dev knows something about this.

@jgallen23
Copy link
Author

Thanks for your help. I'm going to re-work my app to use ng-view and templates.

@pkozlowski-opensource
Copy link
Member

Closing this one as this seems to be more a general question than a bug report.

@jgallen23 there is a mailing list dedicated to AngularJS (angular@googlegroups.com), in the future try to ask there is you are not sure if you are dealing with a bug. There are many people present on the mailing list and will be happy to help you.

@jtymes Thnx a lot for helping out!

@jgallen23
Copy link
Author

@pkozlowski-opensource I still think it's a bug that routeProvider doesn't fire events unless an ng-view is in the markup. If nothing else, it should be in the documentation.

@nicolacity
Copy link

I think this should be reopened and addressed. I have a page in which I use the router for hash parsing convenience and to respond to changes in the hashpath, but the information in the path is insufficient for determining which view to load (and I can't put that info in the url as that will clutter it up and disobey our site's url scheme).

I'm using ng-include to load the correct partial once the url is available, but I still want to be able to have the router. Currently this means a hidden element with ng-view on it that loads an empty template on every route, but that shouldn't be necessary in my opinion.

Just let me know if I should file a separate issue for this.

@jtymes
Copy link
Contributor

jtymes commented Dec 17, 2012

@nicolacity It seems to me that you're using the $route service for hash parsing and not for routing? I can see why this would be a problem. The Angular-provided $route service was built to work with ng-view. If it doesn't fit your requirements, then you can write your own service, probably building upon the $location and you can even pull inspiration from the Angular source for the $route service.

If you still think it's a problem with Angular and you are indeed using $route for routing, then maybe you could put together a plunk or a fiddle to demonstrate what the problem is, or what you're trying to achieve?

Thanks!

@brettlaforge
Copy link

I think the plan of extracting url parsing is a good one.
#738

@redmagic
Copy link

I also have a scenario where I use the $route module without binding routes strictly to a view.

My app consist of several gui layers which are shown in several visible/invisible combinations (with ng-if).

One controller is responsible for manipulating the scope for the layer combinations. The route dictates which combinations are shown. This controller is there for the lifetime of the application.

The routing validates the url patterns using resolve. The layer controller listens to the routechange events and adjusts the gui as necessary.

Works like a charm, except for the non used ng-view in the markup.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants