通过AngularJS中的模态编辑对象-使用临时对象
Edit object via modal in AngularJS - use a temporary object?
场景:用户点击项目。以下代码运行并打开一个模态,其中包含一个填充了项目名称的文本框。
$scope.edit = function (item) {
$scope.editingItem = { Name: item.Name };
};
模式中的我的HTML:
<input type="text" ng-model="editingItem.Name"/>
这很好,模态显示(使用ng-show
),文本框填充项目名称。
我使用一个新对象来填充文本框,因为在按下保存按钮之前,我不希望原始对象发生更改(通过AngularJS自动数据绑定)
然后这个HTML:
<a href="" ng-click="update(editingItem)">Save</a>
导致:
$scope.update = function (item) {
// What do I put in here to update the original item object that was passed
// into the edit function at the top of this question?!
};
不过,我的问题是update
方法中要放入什么?我想更新原始的item
(保存在项目数组中)。
我会这样做:
$scope.edit = function (item) {
$scope.editingItem = { Name: item.Name };
$scope.originalItem = item;//store the instance of the item to edit
};
$scope.update = function (item) {
$scope.originalItem.Name = item.Name;//copy the attributes you need to update
//clear temp items to remove any bindings from the input
$scope.originalItem = undefined;
$scope.editingItem = undefined;
};
相关文章:
- 临时Javascript对象
- 哈希被推到猫鼬模型上作为'[对象对象]”;
- 为什么不显示多个HTML对象对象
- javascript对象2对象映射器
- 培根.js Bus.plug: 未捕获 错误: 不是可观察的 : [对象对象]
- 返回“[对象对象] NaN” - Javascript
- 为什么当我在JS数组上调用.toString时得到“对象对象”
- PhantomJS 控制台.log对象输出 [对象对象]
- JavaScript 对象 - 对象值在构造后未定义
- 类型错误:对象 #<对象> 没有方法“身份验证”
- 当我运行代码时,我得到以下结果 []对象对象] [对象对象],但应该给我一个有序数组
- "未捕获[对象对象]”;投掷和接球时
- Handlebars在渲染时部分打印[对象对象]
- JavaScript JSON对象对象错误
- 激活变为翡翠中的[对象对象]
- 迭代我的对象对象的问题
- 如何加载包含对象对象的 JSON 文件的每个部分
- 谷歌图表与表给予“;[对象对象]不符合控制规范”;错误
- Jquery新元素在连接时导致[对象对象]
- 为 JSON 创建临时 Java 对象