Search code examples
angularjsfactoryangular-resource

angular $resource factory pass parameter


I have a factory that makes some $resource calls to the server. The setData function works fine. My question is how do I pass a parameter into the factory to be used in the SubmitAttendance function? I need to pass a id to be sent to the server

.factory('AttendanceData', function ($resource, theConfig) {
    var serverPath = theConfig.apiURL;
    var serverKey = theConfig.apikey;
    return $resource(serverPath + '/api/Canvas/:action',
        {key: serverKey},
        {
            SetData: {method: 'GET', params: {action: "SetData" }, isArray: false},
            SubmitAttendance: {method: 'GET', params: {action: "SubmitAttendance"}, isArray: false}
        });
});

Solution

  • Try instead, returning an object exposing a method which you can throw arbitary data into.

    .factory('AttendanceData', function ($resource, theConfig) {
        return {
            get: function (data) {
                return $resource(/* do stuff with data */)
            }
        }
    });
    

    then in the calling code

    AttendanceData.get('foo');