服务器上有代码
apiRoutes.put('/intake', function(req, res) {
Intake.findById({id, function(err, intake) {
if (err)
res.send(err);
check : true;
intake.save(function(err) {
if (err) {
return res.json({success: false, msg: 'Error'});
}
res.json({success: true, msg: 'Successful update check state.'});
});
}})
});我应该从前端设置ID值,但我不知道如何在函数中设置它。试一下这个apiRoutes.put('/intake',id,function(req,res) ),但是id没有在controller.js的前面定义:
$scope.changeCheck = function(id) {
console.log(id);
mService.intake("PUT", $scope.intake, {"action": "put"}, id)
.success(function(data, status, headers, config) {
}).error(function(err) {
mService.errorHandler(status);
});
};在服务档案中:
intake : function(method, data, params, value) {
var endpoint = "";
switch (params.action) {
case "put" :
endpoint = "intake/" + value;
break;
}
return this.request(method, endpoint, data);
}html
<li ng-repeat="intake in intakes">
<div class="welcome-box">
<div class="welcome-box-content" >
<label class="checkbox">
<input type="checkbox" ng-model="intake.check" ng-change="changeCheck(intake.pres_id)" />
</label>
<span class="drugs"> {{intake.dname}} <br></span> <span class="drugsdescr"><i class="fa fa-comment" aria-hidden="true"> </i> {{intake.comment}} <i class="fa fa-medkit" aria-hidden="true"></i> {{intake.dose1}}{{intake.dose2}} </span>
</div>
</div>
</li>获取入口
mService.intake("GET", "", {"action" : "get"})
.success(function(data, status, headers, config) {
$scope.intakes = data;
console.log(data);
})
.error(function(data, status, headers, config) {
mService.errorHandler(status);
});发布于 2016-06-11 14:38:13
如果我没有弄错,您将在您的角服务中提供id值,作为url的一部分:
endpoint = "intake/" + value;结果是这样的:intake/12345
因为这里没有使用查询参数,所以服务器将把它作为url的一部分。
因此,您必须在服务器上指定id是url的一部分:
'/intake/:id‘
apiRoutes.put('/intake/:id', function(req, res) {
...
});然后您可以从请求中获得id值:
req.params.id因此,服务器上的put函数应该如下所示:
apiRoutes.put('/intake/:id', function(req, res) {
var id = req.params.id;
Intake.findById({id, function(err, intake) {
if (err)
res.send(err);
check : true;
intake.save(function(err) {
if (err) {
return res.json({success: false, msg: 'Error'});
}
res.json({success: true, msg: 'Successful update check state.'});
});
}})
});https://stackoverflow.com/questions/37763764
复制相似问题