【问题标题】:Passing multiple parameters from controller to factory in angularjs在angularjs中将多个参数从控制器传递到工厂
【发布时间】:2014-07-29 12:48:27
【问题描述】:

我按照以下方式将三个参数从我的控制器传递到工厂。

在我的控制器中,我尝试传递三个参数 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;
        });
    };

在我的工厂里有

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

当我运行此程序时,我收到错误请求错误。请让我知道如何传递多个参数。谢谢

【问题讨论】:

  • 在浏览器调试器控制台中查看网络调用。查看正在发送的参数,您可能会对错误有所了解。

标签: javascript angularjs


【解决方案1】:

这很好用,假设您希望将查询字符串中的 :id 替换为 $scope.id 的值,并附加两个查询参数(sdt 和 edt),如下所示:

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

您可能希望得到一个如下所示的 URL:

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

...在这种情况下,您的代码应如下所示:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 2016-09-12
    • 2016-01-13
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    相关资源
    最近更新 更多