javascript - 在这种情况下,我是否需要在 angularjs 中手动管理范围?
问题描述
我正在(重新)学习 angularjs,因为我需要将一些现有代码迁移到另一个框架。
我有一个控制器,负责管理在表格中呈现的保存的收藏夹列表。收藏夹被保存到 mongodb 集合中。它看起来像这样:
export const favoritesController = ($scope, getFavorites, removeFavorites) => {
getFavorites.then(favs => {
$scope.favorites = favs;
});
// Called when a button is clicked
// to remove selected favorites from a table.
$scope.removeFavorite = ($event) => {
let ids = [];
$("#favorites input:checked").each((i, el) => {
ids.push(parseInt(el.value));
});
removeFavorites(ids);
// I know, we should ensure the
// scope management occurs only
// when and if the
// favs were successfully deleted.
$scope.favorites = $scope.favorites.filter(
favorite => {
return !ids.includes(favorite.id);
});
};
}
如果我只是调用该服务从 db ( removeFavorites
) 中删除收藏夹,则范围不受影响,因此收藏夹不会从 UI 中的显示表中删除。我还必须从$scope
. 那是对的吗?是否有其他方法可以连接范围变量和数据库上的操作,或者我是否正确必须手动完成?
解决方案
推荐阅读
- c# - 使用 DataTable(ASP.NET MVC Web 应用程序)生成具有多个行标题的 html 表
- python - 为什么在写入 csv 文件以及添加到列表时,数字字符串和字符串文本(波斯语)的顺序会发生变化?
- botframework - 使用 Direct Line(不是 WebChat)在 LivePerson 中托管的 Microsoft Bot(使用框架 v4)未触发欢迎消息
- html - 如何在 ngfor 中设置 ngmodel
- node.js - NW.js 无法在 Sdk 和普通风味之间切换错误:rcedit.exe 失败,退出代码为 1
- python - 在heroku中部署whatsapp bot不起作用
- parallel-processing - CUDA 平铺矩阵乘法解释
- accessibility - 对于可访问的网站设计,表格还是不表格?
- flutter - Flutter GridView 在模态底部工作表中?
- selenium - Edge 的 selenium webdriver 问题,找不到文件...或 ID 为“isDisplayed.js”的嵌入式资源