Angular2:如何编辑字段

Angular2: How to edit a field

我正在构建联系人列表应用程序,用户可以在其中使用输入字段添加联系人,就像我们通常为获取数据所做的那样,我已完成添加和删除联系人,但我对如何编辑添加的联系人感到困惑单击添加联系人旁边的按钮,情况是我单击编辑按钮,联系人将出现在输入字段中,我将在其中编辑联系人并单击更新按钮,编辑后的联系人将返回列表中联系人,我可能会得到 -ve 投票,但我真的很想看看它会如何发生,就像联系人返回输入字段进行编辑一样。

这是我为它写的它以id为参数

updateparent(value){
    for(var i = 0; i < this.array.length ; i++){
    console.log('inside for');
    if(value == this.array[i].idobj){

        break;    
    }
}

如果之后是什么? :)

HTML 父级:

<h1 class= "text-center">ToDo App</h1>
<div class = "form-group">
<lable>Task</lable>
<input name = "tasks" #task class = "form-control" >
<lable>Detail</lable>
<input type = "text" name = "taskdetail" #detail class = "form-control"  >
<button type = "submit" class  = "btn btn-default" 
(click) = "addtask(task, detail)">Add Task</button>
<child-component *ngFor = "#todo of array" 
[taskobject] = "todo"   (childevent) = "deleteparent($event)">
Loading...   </child-component>
</div> 

HTML 子组件显示从父组件捕获的数据:

{{taskobject.taskobj}}
{{taskobject.detailobj}}
{{taskobject.idobj}}
<button type = "button" class = "btn btn-default" 
(click) =   "deletetask(taskobject.idobj)">Delete</button>
<button type = "button" class = "btn btn-defualt"
(click) = "updatetask(taskobject.idobj)">Update</button>

`

如果您的 contact number 只是数组中的一个值,则匹配联系人的现有值以及当您获得索引值(或 i)时 对于匹配 splice 数组中的值,然后使用 push 方法更新数组中的新值。 像这样:

updateparent(value){
    for(var i = 0; i < this.array.length ; i++){
    console.log('inside for');
    if(value == this.array[i].idobj){
        this.array.splice(i,1);
        var newContact = value
        this.array.push(value);     
        break;    
    }
}

希望对您有所帮助!如果不提供 plnkr 代码,我会改正它!