0

i have a challenge with routing on angularJS to conform with the base url set in the config on code igniter

base url = http://localhost:8080/example/

for example if my current url is

http://localhost:8080/example/details

and there's angular service call from the Angular controller of that page like this

$http.get("api/gallery/" + 4).success(function (data) {
        galleryResource.getAll().$promise.then(function (response) {
            console.log(response);
        }, function (response) {
            console.log(response);
        });

the url called will be

http://localhost:8080/example/api/gallery/4

but when the url is now

http://localhost:8080/example/details/4

and thesame service is called the url that will be called will be

http://localhost:8080/example/details/api/gallery/4

my question now is, is there a way to ensure that api url does not follow the current url in the address bar for starts from the base url because with the example above i want the api url to be

http://localhost:8080/example/api/gallery/4

while am on the page with url

http://localhost:8080/example/details/4 Thanks in anticipation of your responses``

1 Answer 1

0

You have to get your base url or to set it as a constant in AngularJS.

something like app.constant('base_url', 'http://localhost:8080/example');

then in your javascript, the url transforms from

$http.get("api/gallery/" + 4) 

to

$http.get(base_url + "api/gallery/" + 4)

here is a more detailed example of how to define an constant: AngularJS constants

Basically you need to set a base url for your API and use it on every request. This is also useful because if you change the url of the API, maybe you separate the api from the frontend something like api.localhost.com and localhost.com , you can change your base_url to api.localhost.com and everything will work just fine.

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

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.