AngularJS Module to define dynamic variables in ngResource defintion like familiar with in templates. Its has the same bracket syntax for exampel {{config.value}} could also be used in the ngResource url definition and will be replaced with the correspondant value when the request is performed.
- AngularJS 1.1.4 + (tested with 1.1.4)
##Usage
- download js file
- added javascript file to your app html file
<script type='text/javascript' src="angularjs-interpolate-interceptor.js"></script>
- add module to the app.js and register interpolateInterceptor as http interceptor
var appModule = angular.module('app', ['angularjs-interpolate-interceptor'])
appModule.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('interpolateInterceptor');
}])
That done. Now you can use the angularjs bracket syntax to define variables in the nrResource service url definition. For example
angular.module('Services', ['ngResource'], function ($provide) {
$provide.factory('ServiceA', function ($resource, $rootScope) {
var resource = $resource('/{{config.valuea}}/', {}, {
get: {method: 'GET', params: {}},
});
return resource;
});
});
The variable {{config.valuea}}
var resource = $resource('/{{config.valuea}}/', {}, {
- Report at the github issue tracker
- at the moment the variable is hardcoded with name config and has to be registered on the $rootScope make it configurable
request: function (config) {
var exp = $interpolate(config.url);
config.url = exp({config: $rootScope.config})
return config;
}
- support variable in url of ngResource service definition
missing
##Example
Define on the $rootScope the config object amd use it on the service defintion like
appModule.run(function ($rootScope) {
$rootScope.config = {valuea:'test', valueb:'prod'}
});
angular.module('Services', ['ngResource'], function ($provide) {
$provide.factory('ServiceA', function ($resource, $rootScope) {
var resource = $resource('/{{config.valuea}}/', {}, {
get: {method: 'GET', params: {}},
});
return resource;
});
});
Two ways to run the demo app local one with play or second with nodejs.
Dependencies
- play 2.2.3 (optional)
- nodejs 0.10.28
- (karma-test runner)[http://karma-runner.github.io/0.12/intro/installation.html]
Start it with play
play run
Then go to
Start it with nodejs
node server.js
Then go to
Or run the karma test local with
npm test
The motivation is to support dynamic variables for ngResource service defintion to change urls at runtime for example or to inject global variables. Maybe it could also be used to minimize the controller logic to only bind data for the view and the service defined which variable he need for his request. It is a young project and started as proof of concept maybe they can support other uise case. Feel free to try and develop it to support your needs. Questions, thanks, ideas contact me.
angularjs-interpolate-interceptor is released under the MIT License.