2

I am new to angularjs but have been working with django for some time.

I am used to the idea of writing the following in my templates:

<a href="{% url 'profile-page' user.pk %}">{{ user.name }}</a>

and the following being generated in the rendered html:

<a href="/profiles/33">Eamonn</a>

n.b. this profile-page url is defined in my url routing

This is very powerful as I can change the urls without having to change the templates.

I am loving angularjs but I am not too happy adding my urls into my templates and I am using a name instead of the url which feels like I am programming to an interface and not an implementation. Also, if I specify a url two or three times in different templates it is not very DRY.

Is there any native way of doing this of something similar in angularjs?

3
  • If angularjs do not have nice utility functions for urls, then render them outside of template, and pass as variables Commented Mar 11, 2014 at 9:59
  • Have you looked into ngHref ? Also, please, keep in mind that Angular is a JS framework (which means it does it's stuff in the client, so it parses the templates after it reaches the client. Commented Mar 11, 2014 at 10:12
  • ExpertSystem: ng-href is js that runs client side. I cannot see any difference to having ng-href run with a hard coded url or a named url except the named urls gives better flexibility. Commented Mar 11, 2014 at 10:23

3 Answers 3

1

I would create a constant in your app and then inject that into either the $rootScope, or controller that looks after the specific part of the page with the links.

Something like this:

app.constant('appURL', {
  login:             '/login'
  userProfilePrefix: '/profiles/'
});

Then your controller can request it as a dependency and put it on the scope (or you could put it on the $rootScope in app.run(..) :

app.controller('PageController', ['appURL', function($scope){

  $scope.appURL = appURL;
  $scope.userId = 33;

}]);

Finally, your HTML can use the following:

<a ng-href="{{appURL.userProfilePrefix + userId}}">Eamonn</a>

NOTE use ng-href where possible - all this does is means that if a user happens to click on a link before the interpolation has happened, they won't get redirected to /{{appURL.userProfilePrefix + userId}}

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. This looks like a good solution but I think will end up being a lot of code as the route names are not with the routes. Thanks for the time on ng-href too!
0

I found this library that does exactly what I want: https://github.com/airtonix/angular-named-routes

Thanks for your responses.

Comments

0

I've created a module to solve exactly this problem.

Here's the issue at AngularJS github: https://github.com/angular/angular.js/issues/7091#issuecomment-40742173

And here's the module repository: https://github.com/betsol/angular-router

This module works on top of this library: https://github.com/betsol/js-router

Usage example

routerProvider
    .when('/user/:userId/post/:postId/comment/add', {
        name: 'user-post-comment-add',
        templateUrl: '/partials/user-post-comment-add.html',
        controller: 'UserPostCommentAddCtrl'
    })
;
<a href="{{ router.generate('user-post-comment-add', { userId: 100, postId: 500 }) }}">Add comment</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.