简体   繁体   English

在angularjs中将多个参数从控制器传递到工厂

[英]Passing multiple parameters from controller to factory in angularjs

I am passing three parameters from my controller to the factory following way. 我正在从控制器向工厂传递三个参数。

In my controller I am trying to pass three parameters id,sdt and edt .. 在我的控制器中,我试图传递三个参数id,sdt和edt ..

 $scope.val = function () {
        $scope.tech = techRepository.getTech.query({ id: $scope.id, sdt: $scope.sDate, edt: $scope.eDate }, function(data) {
            scope.tech = data;
        });
    };

In my factory I have 在我的工厂,我有

App.factory('techRepository', ['$resource', function ($resource) {
    return {
       getTech: $resource('/api/Tech/GetRange/:id', {id: '@id', start: '@sdt', end: '@edt'}, {query: {method: 'GET', isArray:true}})
    };
}]);

When I run this I get Bad Request error. 当我运行这个时,我得到Bad Request错误。 Please let me know how to pass multiple parameters. 请告诉我如何传递多个参数。 Thanks 谢谢

This works fine, presuming you want :id in your query string to be replaced with the value of $scope.id , and two query parameters ( sdt and edt ) attached like: 这样可以正常工作,假设你想要:id你的查询字符串中的:id将替换为$scope.id的值,并附加两个查询参数( sdtedt ):

http://www.example.com/api/Tech/GetRange/123?edt=20140610&sdt=20140609

It seems like you may instead be expecting a URL that looks like: 您似乎可能期望看起来像这样的URL:

http://www.example.com/api/Tech/GetRange/123?end=20140610&start=20140609

... in which case, your code should look like: ...在这种情况下,您的代码应如下所示:

// in controller
$scope.val = function () {
    $scope.tech = techRepository.getTech.query({ id: $scope.id, start: $scope.sDate, end: $scope.eDate }, function(data) {
        scope.tech = data;
    });
};

.factory('techRepository', ['$resource', function ($resource) {
    return {
        getTech: $resource('/:id', {id: '@id'}, {query: {method: 'GET', isArray:true}})
    };
}]);

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM