angularjs - Is it possible to use parameterized URL templates with angular $http service -


i'm using $resource restful api's , love parameterized url template example 'api/clients/:clientid'

this works great crud operations. of api's reports or read-only end points without need full restful treatment. felt overkill use $resource , instead used custom data service $http.

the drawback lose parameterized url templates. love define url like'api/clients/:clientid/orders/:orderid' , pass { clientid: 1, orderid: 1 }. realize can build url dynamically hoping $http supported parameterized template , haven't found yet.

all best

update 7/5

the word missing in searches 'interpolate'. more information comes when search 'url interpolation in angular $http'. short answer looks 'no' $http doesn't support url interpolation. there few easy ways accomplish however.

1. use $interpolate:

documentation $interpolate here

var exp = $interpolate('/api/clients/{{clientid}}/jobs/{{jobid}}', false, null, true); var url = exp({ clientid: 1, jobid: 1 }); 


2. write own url interpolation function

ben nadel has great post on exact topic here.


3. steal functionality right out of angular-resource

check out seturlparams on route.prototype in angular-resource.js. straightforward.

sample data service using $interpolate

(function () {     'use strict';      var serviceid = 'dataservice.jobsreports';      angular.module('app').factory(serviceid, ['$http', '$interpolate', function ($http, $interpolate) {          var _urlbase = 'http://localhost:59380/api';         var _endpoints = {             getjobsbyclient: {                 url: 'clients/{{clientid}}/jobs',                 useurlinterpolation: true,                 interpolatefunc: null             }         };          // create interpolate functions when service instantiated         angular.foreach(_endpoints, function (value, key) {             if (value.useurlinterpolation) {                 value.interpolatefunc = $interpolate(_urlbase + '/' + value.url, false, null, true);             }         });          return {             getjobsbyclient: function (clientid) {                 var url = _endpoints.getjobsbyclient.interpolatefunc({ clientid: clientid });                 return $http.get(url);             }         };      }]);  })(); 

to prevent being "unanswered" when has been answered ...

1. use $interpolate:

documentation $interpolate here

var exp = $interpolate('/api/clients/{{clientid}}/jobs/{{jobid}}', false, null, true); var url = exp({ clientid: 1, jobid: 1 }); 


2. write own url interpolation function

ben nadel has great post on exact topic here.


3. steal functionality right out of angular-resource

check out seturlparams on route.prototype in angular-resource.js. straightforward.

sample data service using $interpolate

(function () {     'use strict';      var serviceid = 'dataservice.jobsreports';      angular.module('app').factory(serviceid, ['$http', '$interpolate', function ($http, $interpolate) {          var _urlbase = 'http://localhost:59380/api';         var _endpoints = {             getjobsbyclient: {                 url: 'clients/{{clientid}}/jobs',                 useurlinterpolation: true,                 interpolatefunc: null             }         };          // create interpolate functions when service instantiated         angular.foreach(_endpoints, function (value, key) {             if (value.useurlinterpolation) {                 value.interpolatefunc = $interpolate(_urlbase + '/' + value.url, false, null, true);             }         });          return {             getjobsbyclient: function (clientid) {                 var url = _endpoints.getjobsbyclient.interpolatefunc({ clientid: clientid });                 return $http.get(url);             }         };      }]);  })(); 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -