Reputation: 1214
Before I start, I am using a .Net back end.
From the server I want to send app specific details such as api end points to the angular app to use to populate the page with data. Thses end points will be sent to the client side angular app.
I have an api and the api locations may change from time to time so I want this to be handled in the back end. If these are changed I don't want to have to change my front end code.
So before I can hit these apis I need to know the end points.
What is the best way to bootstrap my app to have these available before I query the api.
Upvotes: 0
Views: 46
Reputation: 5542
You can have a static endpoint in your backend's API that would return all the end points and then use them on your angular app
Alternatively you can have a script tag on your body:
<script type="text/javascript">
//add your variables
var backendVariable1 = @(myVar1); //razor syntax?
var backendVariable2 = @(myVar2);
</script>
And then, somewhere inside your angular app (it could be in a service for example):
angular.module('myApp').factory('BackendVariables',
function() {
this.variables.variable1 = window.backendVariable1;
this.variables.variable2 = window.backendVariable2;
return this.variables;
}
);
Upvotes: 1
Reputation: 1919
Create an angular service for holding configuration items such as endpoints.
Poll the server to get the endpoints while initialising the service (the endpoint for this call needs to be in the front-end at least e.g. "/api/endpoints" unless you are generating a global on the serverside somehow).
You can then inject the service to any module you'd like; to get the endpoints.
Upvotes: 1