angular - 如何将参数传递给 Angular 中的模态

我正在用 Angular 做一个小型的博客写作项目来练习。我将使用删除按钮在屏幕上显示所有文章卡。当用户按下删除按钮时,应打开一个模式要求确认。这是代码:

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>&nbsp;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>

截图如下:

这是我的逻辑: 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");
      }
    );
  }
}

但是在按下 Yes, sure 按钮时我收到了这个错误:

ERROR TypeError: _co.article is undefined

我知道原因。实际上 (click)="display=false; onPress(article.articleid)" 在模态中不知道 article.articleid

如何在那里传递 articleid。请帮我。我的整个逻辑是错误的吗?

最佳答案

您可以设置一个变量来存储被点击的帖子的 ID。因此,在单击删除时将 Id 分配给该变量,并在确认后使用它来删除帖子

文章.组件.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(article.Id)" class="btn btn-danger"><i class="fa fa-trash"></i>&nbsp;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(true)" label="Yes">Yes, Sure</button>
        <button ... (click)="display=false" onPress(false) label="No">No, leave it!</button>
    </p-footer>
</p-dialog>

文章.组件.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;
  deletePostId:number;
  showDialog(id) {
      this.display = true;
      this.deletePostId=id
  }   
 
  ngOnInit() {
    // logic to populate this.articles[] array 
  }

  onPress(isDelete) { 
    if(isDelete && deletePostId)  {
    this._articleService.deleteArticle(this.deletePostId)
    .subscribe (
      data => {
        console.log("deleted");
      }
    );
 }else{
  this.deletePostId = null;
 }     

  }
}

https://stackoverflow.com/questions/64176081/

相关文章:

kubernetes - 使用 Persistent Volume Claim 时是否必须显式创建

postgresql - Ecto:如何用不同的随机数更新所有记录

reactjs - @apollo/client error-React Hook "useQuer

arrays - 使用 jq 将两个 JSON 数组合并到一个文件中

r - 制作数据框每一列的向量并返回列表中的向量

r - 如何在保留 R 中的某些变量的同时将长整形为宽

python - 排序函数中 x%2==0 的排序列表给出了我不希望的结果

python - 如何划分字典值?

javascript - 使用 React.js 时未定义 Web Speech API Speec

swiftui - 编辑 : How we can alignment Image with Ima