A very rookie-ish question:
I'm trying to build resource object using factory method:
.factory('Magazines', [function ($resource) { var url = document.URL; var urlArray = url.split("/"); var organId = urlArray[urlArray.length-1]; return $resource('http://localhost/ci/api/magazines/:id', { loginID : organEntity, password : organCommpassword, id : organId }); }])
This method is easy because all params are predefined, organEntity and organCommpassword are defined inside tag.
Now for a different resource object, I need to pass in parameter when the factory is called.
I imagine the calling code of this resource object should look like:
.controller('ResrouceCtrl', function($scope, Magazines) { $scope.magazines = Magazines.query(); });
I know query() method can add parameters: Magazines.query(params, successcb, errorcb);
I wonder if I just pass in parameters, can I get the parameter at the factory? How to specify such passed in parameters in the factory method?
For example, now suppose I cannot get organId from url anymore, I need to pass it in from my controller, how to receive organId within the factory method?
Here is my resource js:
.factory('MagComments', function ($resource) { return $resource('http://localhost/dooleystand/ci/api/magCommenct/:id', { loginID : organEntity, password : organCommpassword, id : '@magId' //pass in param using @ syntax }); })
Here is my controller:
$scope.magComments = MagComments.query({magId : 1});
I tried to pass in the parameter, but it causes an error
To use this function you just need to create two NameValueCollections holding your parameters and request headers. Show activity on this post. You can also pass value directly via URL. If you want to call method public static void calling(string name){....}
get() method. Using the params property we can pass parameters to the HTTP get request. Either we can pass HttpParams or an object which contains key value pairs of parameters.
I think I see your problem, you need to use the @
syntax to define parameters you will pass in this way, also I'm not sure what loginID or password are doing you don't seem to define them anywhere and they are not being used as URL parameters so are they being sent as query parameters?
This is what I can suggest based on what I see so far:
.factory('MagComments', function ($resource) { return $resource('http://localhost/dooleystand/ci/api/magCommenct/:id', { loginID : organEntity, password : organCommpassword, id : '@magId' }); })
The @magId
string will tell the resource to replace :id
with the property magId
on the object you pass it as parameters.
I'd suggest reading over the documentation here (I know it's a bit opaque) very carefully and looking at the examples towards the end, this should help a lot.
I suggest you to use provider
. Provide is good when you want to configure it first before to use (against Service/Factory)
Something like:
.provider('Magazines', function() { this.url = '/'; this.urlArray = '/'; this.organId = 'Default'; this.$get = function() { var url = this.url; var urlArray = this.urlArray; var organId = this.organId; return { invoke: function() { return ...... } } }; this.setUrl = function(url) { this.url = url; }; this.setUrlArray = function(urlArray) { this.urlArray = urlArray; }; this.setOrganId = function(organId) { this.organId = organId; }; }); .config(function(MagazinesProvider){ MagazinesProvider.setUrl('...'); MagazinesProvider.setUrlArray('...'); MagazinesProvider.setOrganId('...'); });
And now controller:
function MyCtrl($scope, Magazines) { Magazines.invoke(); .... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With