raju
raju

Reputation: 4978

How to use angular factory, $resource

I am building my first app with angular and currently I have my service defined as

angular.module('mean.testruns').factory('Testruns', ['$resource', function($resource) {
    return $resource('testruns/:testrunId', {
        testrunId: '@_id'
    }, {
        update: {
            method: 'PUT'
        }
    });
}]);

I have added another url on the rest server as

'/testcases/:testcaseId/testruns'

How do I include this in the above testruns factory function?

I currently have my controller as

$scope.findOfTestcase = function() {
    //Need to correct this
    Testruns.query({testcaseId:$stateParams.testcaseId}, function(testruns) {
        $scope.testruns = testruns;
    });
};

$scope.findOne = function() {
    Testruns.get({
        testrunId: $stateParams.testrunId
    }, function(testrun) {
        $scope.testrun = testrun;
    });
};

Upvotes: 0

Views: 84

Answers (1)

link
link

Reputation: 1676

Not totally sure about this, but you could try something like:

angular.module('mean.test').factory('Testruns', ['$resource', function($resource) {

    return {
        runs: $resource('testruns/:testrunId', ...),
        cases: $resource('testcases/:testcaseId', ...)
    }
}]);

And use it like this:

app.controller('ctrl', ['$scope', 'Testruns', 
    function($scope, Testruns) {
        $scope.testCases = Testruns.cases.query();
    }
])

Upvotes: 2

Related Questions