0

我正在构建联系人列表应用程序,用户可以使用输入字段添加联系人,就像我们通常用于获取数据一样,我已完成添加和删除联系人,但我对如何通过单击旁边的按钮编辑添加的联系人感到困惑添加的联系人,情况是我点击编辑按钮,联系人将在输入字段中,我将编辑联系人并点击更新按钮,编辑后的联系人将回到联系人列表中,我可能会得到-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>

`
4

1 回答 1

0

如果您contact number只是数组中的一个值,则匹配联系人的现有值,并且当您splice从数组中获得索引(或 i)的值以匹配该值时,然后使用 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 代码,我会改正的!

于 2016-02-06T13:28:10.343 回答