I'm making a small blog writing project in Angular for practice. I will show all article card on screen with delete button. When the user press on delete button a modal should open asking for confirmation. Here's the code:
articles.component.html
<!-- CARD -->
<div class="row mt-5">
<div class="col-md-4 mb-3" *ngFor="let article of articles; let i = index;">
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">{{article.title}}</h5>
<a (click)="showDialog()" class="btn btn-danger"><i class="fa fa-trash"></i> Delete</a>
</div>
<div class="card-footer text-muted">
{{article.date}}
</div>
</div>
</div>
</div>
<!-- MODAL -->
<p-dialog header="Deleting this article!" [(visible)]="display" [modal]="true" [responsive]="true" ...>
<span>Are you sure?</span>
<p-footer>
<button ... (click)="display=false; onPress(article.articleid)" label="Yes">Yes, Sure</button>
<button ... (click)="display=false" label="No">No, leave it!</button>
</p-footer>
</p-dialog>
And here is my logic: article.component.ts
import { Component, OnInit } from '@angular/core';
...
@Component({
...
})
export class ArticleComponent implements OnInit {
articles = []; // contains title, date, articleid
constructor(..., private _articleService: ArticleService) { }
display: boolean = false;
showDialog() {
this.display = true;
}
ngOnInit() {
// logic to populate this.articles[] array
}
onPress(id) {
this._articleService.deleteArticle(id)
.subscribe (
data => {
console.log("deleted");
}
);
}
}
But on pressing Yes, sure button I'm getting this error:
ERROR TypeError: _co.article is undefined
I know the reason. Actually (click)="display=false; onPress(article.articleid)" in modal have no idea about article.articleid.
How to pass articleid there. Please help me. Is my entire logic wrong?
