How will be able to delete item in Angular 2+? I am new to Angular and I'm confused a little bit about this. Hope you guys can help. I just did the add item by the way but i don't know how to delete an item. I am using three files here below. 1. cribs.service.ts, 2. crib-card-components.ts and 3. crib-card-component.html.
//cribs.service.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Subject } from 'rxjs/Subject'
import 'rxjs/add/operator/map';
@Injectable()
export class CribsService {
public newCribSubject = new Subject<any>();
constructor(private http: Http) { }
getAllCribs() {
return this.http.get('data/cribs.json').map(res => res.json())
}
addCrib(data){
data.image = 'default-crib';
this.newCribSubject.next(data);
}
}
//crib-card-component.ts
import { Component, OnInit, Input } from '@angular/core';
import { Crib } from './../crib';
@Component({
selector: 'app-crib-card',
templateUrl: './crib-card.component.html',
styleUrls: ['./crib-card.component.css']
})
export class CribCardComponent implements OnInit {
@Input('crib') crib: Crib;
constructor() { }
ngOnInit() {
}
}
//crib-card.component.html
<div class="thumbnail">
<img src="assets/images/{{ crib.image }}.jpg" alt="">
<div class="caption">
<div *ngIf="!crib.showDetails">
<h4>
<span class="label label-primary">
{{ crib.type }}
</span>
</h4>
<h3>
<i class="glyphicon glyphicon-tag"></i>
{{ crib.price | currency: 'USD':true }}
</h3>
<h4>
<i class="glyphicon glyphicon-home"></i>
{{ crib.address }}
</h4>
<hr>
<button
class="btn btn-sm btn-success"
*ngIf="!crib.showDetails"
(click)="crib.showDetails = !crib.showDetails">
Details
</button>
<button
class="btn btn-sm btn-danger"
(click)="removeItem(crib)">
Delete
</button>
</div>
<div *ngIf="crib.showDetails">
<div class="details">
<h4>
<span class="label label-primary">
Beds: {{ crib.bedrooms }}
</span>
<span class="label label-primary">
Baths: {{ crib.bathrooms }}
</span>
<span class="label label-primary">
SqFt: {{ crib.area }}
</span>
</h4>
<p>{{ crib.description }}</p>
<hr>
<button
class="btn btn-sm btn-danger"
(click)="crib.showDetails = !crib.showDetails">
Close
</button>
</div>
</div>
</div>
</div>