从函数中为工厂参数赋值?

时间:2015-04-28 04:48:03

标签: javascript angularjs

我在app.factory中有这段代码,stardate and enddate是我完成请求的参数GET

app.factory('Incident', function ($resource) {
    return $resource('http://incidents-core/app_dev.php/:path?limit=8&startDate=:startdate&endDate=:enddate&order=orderBy=id', {
        path: '@path'
    }, {
        getIncidents: {
            method: "GET",
            params: {
                path: 'incidents',
                startdate: '@startdate',
                enddate:'@enddate',
            },
            isArray: false
        }
    })

我使用这个功能:

app.controller("IncidentIndexCtrl", function ($resource,$scope, Incident, Device, $http, $window) {

    Incident.getIncidents(function (data) {
        $scope.incidents = data._embedded.incidents;
    });

        $scope.substract = function(){   
            substract = document.getElementById("substractValue").value;
                var actualdate = new Date();
                var DD = actualdate.getDate();
                var MM = actualdate.getMonth()+1;
                var YYYY = actualdate.getFullYear();
                var date = new Date();
            date.setDate(date.getDate() - substract);
                var dd = date.getDate();
                var mm = date.getMonth()+1;
                var yyyy = date.getFullYear();
            return console.log('Actual Date: ',actualdate, DD, MM, YYYY,  'Result: ', date, dd, mm, yyyy);
    };

输入index.html

<input id="substractValue" type="text"><button type="submit" ng-click="substract()">substract</button>

但我不知道如何将值赋给参数,只是我想知道如何传递值,我可以继续处理我的函数

1 个答案:

答案 0 :(得分:0)

快速回答,将参数作为资源操作的第一个参数传递,例如

Incident.getIncidents({
    startdate: someDate,
    enddate: anotherDate
}, function success(data) {...});

someDateanotherDate是您需要的字符串参数。

我可能会重新构建您的资源,因为您不必要地将查询变量添加到资源网址

return $resource('http://incidents-core/app_dev.php/:path', {
    path: '@path'
}, {
    getIncidents: {
        method: "GET",
        params: {
            path: 'incidents',
            limit: 8,
            order: 'orderBy=id'
        },
        isArray: false
    }
})