最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing multiple parameters from controller to factory in angularjs - Stack Overflow

programmeradmin2浏览0评论

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 ..

 $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. Please let me know how to pass multiple parameters. Thanks

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 ..

 $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. Please let me know how to pass multiple parameters. Thanks

Share Improve this question asked Jun 9, 2014 at 1:12 J. DavidsonJ. Davidson 3,31714 gold badges56 silver badges105 bronze badges 1
  • See the network call in browser debugger console. See what parameters are being send you may get some idea about the error. – Chandermani Commented Jun 9, 2014 at 2:20
Add a ment  | 

1 Answer 1

Reset to default 4

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:

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

It seems like you may instead be expecting a URL that looks like:

http://www.example./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

发布评论

评论列表(0)

  1. 暂无评论