我正在尝试构建一个简单的 CRUD 应用程序,用于使用 AngularJs 管理员工任务 MongoDb 和 NodeJs。由于某种原因,当 $http 请求从 Angular 到 Node 时,范围变量在执行 CRUD 操作时不会更新。
首先我使用以下方法获取任务列表:
$http.get("http://localhost:3000/employees-tasks")
.success(function (response) {
$scope.arrEmployeesTasks = response;
});
假设我想删除员工的任务 - 我正在使用以下功能:
AngularJs:
$scope.deleteTask = function (prmTaskObj) {
$http.delete("http://localhost:3000/delete-task?_id=" + prmTaskObj._id).success(function (response) {
console.log(response);
});
Node Js
exports.delete = (req, res) => {
Tasks.findByIdAndRemove(req.query._id)
.then(task => {
if(!task) {
return res.status(404).send({
message: "task not found with id " + req.query._id
});
}
res.send({message: "task deleted successfully!"});
}).catch(err => {
if(err.kind === 'ObjectId' || err.name === 'NotFound') {
return res.status(404).send({
message: "task not found with id " + req.query._id
});
}
return res.status(500).send({
message: "Could not delete task with id " + req.query._id
});
});
};
重要的是要补充一点,CRUD 操作有效...但我无法让 $scope.arrEmployeesTasks 变量在 UI 中进行相应更新。
最佳答案
Angularjs 摘要循环没有超出 Angular 范围。强制满,您必须使用 $scope.$apply() 执行摘要循环。
关于node.js - 使用 NodeJs 进行增删改查操作后,AngularJs ui 不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54374334/