1

In angular.js, is there a built in way to construct URL based on already specified $routeProvider?

Something like Rails' url_for:

url_for(:action => 'login', :controller => 'account')

or ASP.NET MVC's Url.Action / Url.RouteUrl:

Url.RouteUrl(new { controller = "account", action = "login" })

Or should I just construct the URL manually?

2 Answers 2

1

This is not possible with "bare angular" because Angular only recognizes the URL you're trying to access.

However, Angular-UI's Router allows you to jump to a certain state, thus changing the url based on the state's name.

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

Comments

0

within an angular app, when you click on a link with href path of http://localhost/account/login/. Then Angular steps in, and instead of letting your browser request a page from the server, it intercepts your click event and goes to your routeProvider to see which client-side view to display. Thats why your links work as long as you try not to open them directly.

you can config the routeProvider for this href path http://localhost/account/login/, in angular as shown below.

   app.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
        when('account/login', {templateUrl: 'account/login', controller: 'AccountController'}).
        when('/users', {templateUrl: 'account/users', controller: 'UsersController'}).
        otherwise({redirectTo: '/'});
    }]);

Here in this

when('account/login', {templateUrl: 'account/login', controller: 'AccountController'}).

templateUrl, templateUrl: 'account/login', In ASP.NET MVC, it redirects to account -> Controller and login -> Action.

or you can set to static tempalte like this templateUrl: 'views/user_login.htm'

controller, controller: 'AccountController' is nothing but the AngularJS Controller.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.