Our application has 2-level navigating. We want to use AngularJS $routeProvider
to dynamically provide templates to an <ng-view />
. I was thinking of doing something along the lines of this:
angular.module('myApp', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/:primaryNav/:secondaryNav', {
templateUrl: 'resources/angular/templates/nav/'+<<primaryNavHere>>+'/'+<<secondaryNavHere>>+'.html'
});
}]);
I just don't know how to populate the parts within the <<>>
. I know the primaryNav and secondaryNav get bound to the $routeParams, but how do I access $routeParams here in order to dynamically serve up the template?
Alright, think I got it...
Little background first: The reason I needed this was to stick Angular on top of Node Express and have Jade process my partials for me.
So here's whatchya gotta do... (drink beer and spend 20+ hours on it first!!!)...
When you set up your module, save the
$routeProvider
globally:That may be more info than what was needed...
Basically, you'll wanna store your Module's
$routeProvider
var globally, eg asrouteProvider
so that it can be accessed by your Controllers.Then you can just use
routeProvider
and create a NEW route (you can't 'RESET a route' / 'REpromise'; you must create a new one), I just added a slash (/) at the end so that it is as semantic as the first.Then (inside your Controller), set the
templateUrl
to the view you want to hit.Take out the
controller
property of the.when()
object, lest you get an infinite request loop.And finally (still inside the Controller), use
$location.path()
to redirect to the route that was just created.If you're interested in how to slap an Angular app onto an Express app, you can fork my repo here: https://github.com/cScarlson/isomorph.
And this method also allows for you to keep the AngularJS Bidirectional Data-Bindings in case you want to bind your HTML to your database using WebSockets: otherwise without this method, your Angular data-bindings will just output
{{model.param}}
.If you clone this at this time, you'll need mongoDB on your machine to run it.
Hope this solves this issue!
Cody
Don't drink your bathwater.
Router:-
Controller:-
I believe it is a weakness in angularjs that $routeParams is NOT visible inside the router. A tiny enhancement would make a world of difference during implementation.
This very helpful feature is now available starting at version 1.1.2 of AngularJS. It's considered unstable but I have used it (1.1.3) and it works fine.
Basically you can use a function to generate a templateUrl string. The function is passed the route parameters that you can use to build and return the templateUrl string.
Many thanks to https://github.com/lrlopez for the pull request.
https://github.com/angular/angular.js/pull/1524
I've added support for this in my fork of angular. It allows you to specify
https://github.com/jamie-pate/angular.js/commit/dc9be174af2f6e8d55b798209dfb9235f390b934
not sure this will get picked up as it is kind of against the grain for angular, but it is useful to me
I couldn't find a way to inject and use the
$routeParams
service (which I would assume would be a better solution) I tried this thinking it might work:Which yielded this error:
If something like that isn't possible you can change your
templateUrl
to point to a partial HTML file that just hasng-include
and then set the URL in your controller using$routeParam
s like this:With this as your
urlRouter.html
templateUrl can be use as function with returning generated URL. We can manipulate url with passing argument which takes routeParams.
See the example.
Hope this help.