How to pass parameter to module in angularjs -
i'm looking through angularjs examples, i've found example:
// module cloud persistance in mongolab - https://mongolab.com angular.module('mongolab', ['ngresource']). factory('project', function($resource) { var project = $resource('https://api.mongolab.com/api/1/databases' + '/angularjs/collections/projects/:id', { apikey: '4f847ad3e4b08a2eed5f3b54' }, { update: { method: 'put' } } ); project.prototype.update = function(cb) { return project.update({id: this._id.$oid}, angular.extend({}, this, {_id:undefined}), cb); }; project.prototype.destroy = function(cb) { return project.remove({id: this._id.$oid}, cb); }; return project; });
i don't want using magic string static resource such https://api.mongolab.com/api/1/databases/angularjs/collections/projects/:id
, instead have defined on server , later passed module. question is, how parametrize module, i.e. how pass javascript variable module outside?
you should use provider recipe when want expose api application-wide configuration must made before application starts. interesting reusable services behavior might need vary between applications.
var app = angular.module('mongolab', ['ngresource']) app.provider('project', function projectprovider(){ var resourceurl = false; this.resourceurl = function(url){ this.resourceurl = url; } this.$get = [function project(){ return new project(resourceurl); }]; }); function project(resourceurl) { var project = $resource(resourceurl, { apikey: '4f847ad3e4b08a2eed5f3b54' }, { update: { method: 'put' } } ); project.prototype.update = function(cb) { return project.update({id: this._id.$oid}, angular.extend({}, this, {_id:undefined}), cb); }; project.prototype.destroy = function(cb) { return project.remove({id: this._id.$oid}, cb); }; return project; });
then can config with
app.config(["projectprovider", function(projectprovider){ projectprovider.resourceurl('https://api.mongolab.com/api/1/databases' + '/angularjs/collections/projects/:id') }]);
Comments
Post a Comment