Kar Kar - 2 months ago 6x
reST (reStructuredText) Question

Managing REST endpoints in Angular JS?

Is there a best practice to store and manage all REST endpoints for an application at one location within Angular JS?


You can store them as constants outside application, according to this great document.

You can store such config as


(function (window) {
    window.__env = window.__env || {};

    // Login page address
    window.__env.loginUrl = 'http://website/login';


then assign it as a constant in your main module


angular.module('yourmodule', [])
    .constant('__env', __env)

and then you can inject in in your service

loginData.$inject = ['$http', '__env'];

Such configuration works best for me, is easy to manage and change depending on environment, without rebuilding application. Just assigning constant doesn't have such advantage.