我正在研究Angular4中的一个简单的待办事项应用程序。我有以下方式安装应用程序:
从阵列中删除项目 - 角4
表单组件:这是将项目添加到待办事项列表的形式
项目组件:这是个别组件。
应用程序组件:我有一个* ngFor看看这里列出所有待办事项。
我在应该删除项目的项目组件上添加了一个新按钮,但我无法弄清楚如何“找到”或“找到”要拼接的项目的正确索引号或过滤掉。
我已经包含了链接到github repository here
app.component.html:
<app-navbar></app-navbar>
<div class="container">
<app-form (itemAdded)="onItemAdded($event)"></app-form>
<div class="row">
<div class="col-md-3 mb-3"
*ngFor="let item of toDoList; let i=index">
<app-item
[listItem]="item"
(itemDeleted)="onItemDeleted($event)"></app-item>
</div>
</div>
</div>
app.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
toDoList = [
{
name: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
}
];
onItemAdded(itemData: {itemName: string}){
this.toDoList.push({
name: itemData.itemName
});
}
onItemDeleted(index: number){
this.toDoList.splice(index, 1);
}
}
form.component.html:
<div class="title">
<h1 class="display-4">{{ title }}</h1>
</div>
<div class="input-group">
<input type="text" class="form-control" [(ngModel)]="newItem">
<span class="input-group-btn">
<button class="btn btn-success" type="button" (click)="onAddItem()">
<fa name="plus"></fa>
</button>
</span>
</div>
form.component.ts:
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.css']
})
export class FormComponent implements OnInit {
@Output() itemAdded = new EventEmitter<{itemName: string}>();
title = 'To-Do List';
newItem = '';
constructor() { }
ngOnInit() {
}
onAddItem(){
this.itemAdded.emit({itemName: this.newItem});
}
}
item.component.html:
<div class="task">
<div class="mb-5 d-flex justify-content-between">
<fa name="circle-o" size="3x"></fa>
<button type="button" name="delete" class="btn btn-danger align-self-end" (click)="onDeleteItem()"><fa name="trash"></fa></button>
</div>
{{item.name}}
</div>
item.component.ts:
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-item',
templateUrl: './item.component.html',
styleUrls: ['./item.component.css']
})
export class ItemComponent implements OnInit {
@Input('listItem') item: {name: string};
@Output() itemDeleted = new EventEmitter<{index: number}>();
constructor() { }
ngOnInit() {
}
onDeleteItem() {
this.itemDeleted.emit() //need help with this
}
}
我得到,你可以拼接或过滤出来的顶部答案开始说: “首先,找到你想要删除的元素的索引” 这是我正在试图找出如何做。我想知道索引是基于他们试图删除的项目 –
不要在没有在Stack Overflow上添加资源摘要的情况下链接到外部资源。在这里带上相关的代码。我们不应该访问您的GitHub来帮助您。 – zero298
如果您需要我们的帮助,您能否至少向我们展示您的代码,而不是将我们指向其他地方,我们仍然需要自己搜索您的代码? –