javascript - angular.js $resource as a 'service' with dynamic root -
I have a JS library that I would like to supply to my customers, which is based on glass Defines services:
Angular Modules ('Services', ['Ngrasource']). Factory ('user service', function ($ resource) {return work (whatsonUserId) {return $ resource ('/ rest / user /: action');}}. Factory ... < P> To use my library, my clients only do this: <<> app.controller ('Ctrl', Function (UserService) {... UserService (.) DoSomething () } When my clients use this library, then they use it in their own HTML which is clearly loaded from their URL. The root URL for my services for rabbits goes wrong. Clearly, the method of correcting it will be to supply a full URL for the resource:
Returns $ resource ('www.my .server.url / rest / user /: action '); But I do not have just one' my.server.url '- I have something I have a staging environment I have a development environment, and I have production environments - some are per customer.
My question is this: is there one Examination do I specify the route a normal 'service' Voice 'and' server for users' Services' My Library 'is?
This is a good use case. You can define your API by using module.provider . Define the provider for your server configuration, such as some module Provider ('server config', function () {var serverUrl = 'val'; // default url this.setServerUrl = function (url)) {ServerUrl = url;}}. $ Get = [function () {return} {url: serverUrl}}}}} Once you define such a config service, use it
factory ('user service', function ($ resource, server config) in each of your services {return function (whatsonUserId) {Return $ resource (serverConfig.url + '/ rest) / User The provider needs to configure at the configuration level
myApp.config (["serverConfigProvider", function (serverConfigProvide r) {ServerConfigProvider.setServerUrl ('serverUrlstring');}]);
Comments
Post a Comment