如何使用角度工厂,$资源

时间:2014-05-28 11:31:21

标签: javascript angularjs

我正在使用angular构建我的第一个应用程序,目前我的服务定义为

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

我在其余服务器上添加了另一个网址

'/testcases/:testcaseId/testruns'

如何在上述测试工厂功能中包含它?

我目前的控制器为

$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;
    });
};

1 个答案:

答案 0 :(得分:2)

对此并不完全确定,但您可以尝试以下方式:

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

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

并像这样使用它:

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