在模态(bootstrap ui angular)中编辑现有联系人
Editing an existing contact in modal (bootstrap-ui angular)
我已经建立了一个addressBook持有者,它应该允许用户:
1. create an address, by entering details in modal pop up;
2.delete an address and
3.edit an address that already exists by entering details in modal pop up.
本质上是一个简单的CRUD应用程序,所有数据都使用localStorage
存储。看起来我需要类似于这里的解决方案,但我不能百分之百确定我会怎么做。
我已经实现了地址的创建和删除,但在实现现有地址的编辑时遇到了困难。
使用下面的代码,当我单击编辑联系人按钮时,它会打开模式,但当我将详细信息输入表单时,它将创建一个新地址,而不是编辑我单击的地址。
这是因为我没有区分两者。任何关于如何更改代码以允许我编辑地址的指导都将是非常棒的。我花了几个小时试图弄清楚这一点,但完全被卡住了。请参阅下面的代码。(为代码量道歉,但我认为有必要解决这个问题)
这是javascript
角度工厂
app.factory('addressFactory', function(){
var addressFactory = {};
addressFactory.addressBook = [];
addressFactory.countries = [
{name: "United Kingdom"},
{name: "United States of America"}
];
addressFactory.saveAddress = function(name, country){
addressFactory.addressBook.push({
name: name,
country: country
});
localStorage.setItem('addressBook', JSON.stringify(addressFactory.addressBook));
};
addressFactory.deleteAddress = function(index) {
addressFactory.addressBook.splice(index, 1);
localStorage.setItem('addressBook', JSON.stringify(addressFactory.addressBook));
}
return addressFactory;
})
角度控制器
第一个控制器(testCtrl)
app.controller('testCtrl', function ($routeParams, $uibModal, addressFactory) {
var testCtrl = this;
this.addressBook = addressFactory.addressBook;
this.init = function() {
addressFactory.init();
this.addressBook = addressFactory.addressBook;
}
this.deleteAddress = function(index){
addressFactory.deleteAddress(index);
};
this.open = function () {
testCtrl.modalInstance = $uibModal.open({
templateUrl: 'app/views/partials/form.html',
controller: 'ModalCtrl',
controllerAs:'ctrl',
});
}
this.init();
})
第二个控制器(ModalCtrl)
.controller('ModalCtrl', function ($uibModalInstance,addressFactory) {
this.addressBook = addressFactory.addressBook;
this.countries = addressFactory.countries;
this.saveAddress = function( name, country) {
addressFactory.saveAddress( name,country);
$uibModalInstance.dismiss('cancel');
}
this.cancelAddress = function () {
$uibModalInstance.dismiss('cancel');
};
})
index.html
<!-- used to open a modal to create a new address -->
<a ng-click="ctrl.open()">Enter new address</a>
<div ng-repeat="contact in ctrl.addressBook track by $index">
<p class="contactName">{{contact.name}}</p>
<p class="contactCountry">{{contact.country}}</p>
<!-- used open a modal to edit a new address -->
<button ng-click="ctrl.open()">Edit Contact</button>
<button ng-click="ctrl.deleteAddress($index)">Delete Contact</button>
</div>
form.html
<form>
<input ng-model="ctrl.name" type="text">
<select ng-model="ctrl.country" ng-options="country.name for country in ctrl.countries">
<option value="">--Choose a country--</option>
</select>
<button ng-click="ctrl.cancelAddress()">Cancel</button>
<button ng-click="ctrl.saveAddress(ctrl.name, ctrl.country.name)">Save</button>
</form>
您可以使用testCtrl.modalInstance.idx = index
:将编辑项目的索引传递给模态
app.controller('testCtrl', function ($routeParams, $uibModal, addressFactory) {
this.open = function (index) {
testCtrl.modalInstance = $uibModal.open({
templateUrl: 'form-modal.html',
controller: 'ModalCtrl',
controllerAs:'ctrl',
});
testCtrl.modalInstance.idx = index;
}
...
这是一个代码笔,可以根据项目是否有索引来编辑和保存项目。
相关文章:
- CKEditor Widget-阻止编辑可编辑元素本身
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 高亮显示时编辑文本大小和颜色
- 剑道UI内联编辑:如何在点击其他按钮时隐藏按钮
- 在验证和发送邮件后更改联系人表单的 html
- 将事件聚焦/模糊在可编辑内容的元素上
- 编辑HTML表的源数据
- ExtJS网格单元格编辑器,防止焦点松动问题
- 如何在visualstudio中调试web api时编辑javascript文件
- 具有所有样式的文本正在复制到可编辑文本区域
- 可以't使用PHP使用Froala编辑器上传图像URL
- 用Greasemonkey编辑专栏
- 如何在corona sdk中从CK编辑器中检索数据
- 基于数据类型的编辑框的汇总列表
- 如何使用jQuery可编辑插件检查ajax是否成功完成
- 错误:[$compile:nonassign]表达式'未定义'与指令'内容可编辑'是不可
- 如何在ng-reeat中使用ng-switch来编辑JSON API数据
- 如何获取经过编辑的文本
- 文本编辑后,append函数不适用于文本区域
- 在模态(bootstrap ui angular)中编辑现有联系人