Javascript 从函数中为工厂参数赋值?
我的应用程序中有此代码。工厂,Javascript 从函数中为工厂参数赋值?,javascript,angularjs,Javascript,Angularjs,我的应用程序中有此代码。工厂,stardate和enddate是我完成请求的参数 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'
stardate和enddate
是我完成请求的参数
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>
减法
但是我不知道如何将值分配给参数,我只想知道如何传递值,这样,我就可以继续使用我的函数快速回答,将参数作为第一个参数传递给资源操作,例如
Incident.getIncidents({
startdate: someDate,
enddate: anotherDate
}, function success(data) {...});
其中,someDate
和anotherDate
是您需要的字符串参数
我可能会像这样重新构造您的资源,因为您不需要向资源URL添加查询变量
return $resource('http://incidents-core/app_dev.php/:path', {
path: '@path'
}, {
getIncidents: {
method: "GET",
params: {
path: 'incidents',
limit: 8,
order: 'orderBy=id'
},
isArray: false
}
})