angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Bug: ng-view required for routing

Open jgallen23 opened this issue 14 years ago • 14 comments

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.

jgallen23 avatar Nov 15 '12 18:11 jgallen23

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.

jtymes avatar Nov 15 '12 19:11 jtymes

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

jgallen23 avatar Nov 15 '12 20:11 jgallen23

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.

jtymes avatar Nov 15 '12 20:11 jtymes

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

jgallen23 avatar Nov 15 '12 20:11 jgallen23

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

jtymes avatar Nov 15 '12 20:11 jtymes

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.

jgallen23 avatar Nov 15 '12 20:11 jgallen23

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.

jtymes avatar Nov 15 '12 21:11 jtymes

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

jgallen23 avatar Nov 15 '12 21:11 jgallen23

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 ([email protected]), 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!

pkozlowski-opensource avatar Nov 15 '12 21:11 pkozlowski-opensource

@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.

jgallen23 avatar Nov 15 '12 21:11 jgallen23

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.

nicolacity avatar Dec 16 '12 22:12 nicolacity

@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!

jtymes avatar Dec 17 '12 00:12 jtymes

I think the plan of extracting url parsing is a good one. https://github.com/angular/angular.js/issues/738

brettlaforge avatar Apr 18 '13 17:04 brettlaforge

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.

redmagic avatar Mar 12 '14 13:03 redmagic