【问题标题】:How to pass data to the ng2-bs3-modal?如何将数据传递给 ng2-bs3-modal?
【发布时间】:2017-01-26 22:42:40
【问题描述】:

我有这个来自 *ngFor 的 sn-p,所以它被填充了很多次。每个配置文件都有一个唯一的 ID,当我点击此按钮时,我想将其删除:

<a data-toggle="modal" data-target="#deleteProfile" (click)="deleteProfile.open()" role="button" style="color:red;"><i class="fa fa-trash-o" aria-hidden="true"></i> Delete</a>

html 模态:

    <modal #deleteProfile>
  <modal-header [show-close]="true">
    <h4 class="modal-title">Delete Profile</h4>
  </modal-header>
  <modal-body>
    <div class="text-center">
      Are you sure you want to delete this profile?
    </div>
  </modal-body>
  <modal-footer>
    <div class="control-group confirm-buttons">
      <div class="row text-center">
        <div class="col-md-6">
          <button type="button" class="btn btn-cancel" data-dismiss="modal" (click)="closeDeleteProfile()">No</button>
        </div>
        <div class="col-md-6">
          <button type="button" class="btn btn-confirm" (click)="deleteProfile()">Yes</button>
        </div>
      </div>
      <div class="col-md-12 text-center">
        <small>This is the footer</small>
      </div>
    </div>
  </modal-footer>
</modal>

当点击“是”按钮时调用:

deleteProfile(id: string) {
this.modalDeleteProfile.dismiss();
this.profileService.delete(id)
  .subscribe(
    //  data =>  console.log(data),
    //  error =>  console.log(error)
  );
this.router.navigateByUrl('/dashboard');
}

我如何将 id 传递给模态,以便上面的代码获取 id 以删除配置文件?

这是我正在使用的模式:https://github.com/dougludlow/ng2-bs3-modal

【问题讨论】:

  • 我不知道这是否可能。使用了相同的模态。我只是立即使用了一种额外的方法,它只是几行。所以我没有费心去尝试实现你想要实现的目标。将关注这个问题:)
  • @AJT_82 你能举个例子吗?
  • 使用“额外的方法”?
  • 是的,我找不到解决方案...
  • 检查答案:)

标签: angular ng2-bootstrap


【解决方案1】:

根据 OP 在 cmets 中的要求,这是该问题的替代解决方案。该问题可以通过在组件中添加额外的方法来解决。

你说你有一个*ngFor,它遍历一个数组。与其直接使用按钮打开模态框,不如打开一个方法,该方法传递配置文件的 id,因此您的迭代可能如下所示:

<table>
  <tr *ngFor="let profile of profiles">
    <td>{{profile.id}}</td>
    <td>{{profile.name}}</td>
    <td><button (click)="openDeleteModal(profile.id)">Delete Profile</button</td>
  </tr>
</table>

然后openDeleteModal-方法将在我们将 id 绑定到组件中的局部变量后打开模态窗口。

// declare an extra variable that can be used in deletion
idForDeletingProfile;

openDeleteModal(id) {
  // here we bind the chosen id, so that we can use it in your delete-method
  this.idForDeletingProfile = id;
  this.modalDeleteProfile.open()
}

然后我们有了您的模态,我已将其缩短为仅显示按钮:

<modal #deleteProfile>
  <!-- more code here -->
     <button type="button" class="btn btn-cancel" data-dismiss="modal" (click)="closeDeleteProfile()">No</button>
     <button type="button" class="btn btn-confirm" (click)="deleteProfile()">Yes</button>
  <!-- more code here -->
</modal>

如果用户点击deleteProfile()-按钮,我们之前将选择的id存储在idForDeletingProfile中,现在可以用于删除。

deleteProfile() {
  this.modalDeleteProfile.dismiss();
  // use the local variable here!
  this.profileService.delete(this.idForDeletingProfile)
    .subscribe( data => {
       console.log(data);
       this.router.navigateByUrl('dashboard');
    });
}

【讨论】:

  • 这似乎很容易破解...感谢您为提供您的方法所做的努力。长时间编程时头脑会卡住......
  • 我添加了从 *ngFor 循环中删除配置文件的功能。这是 plunkr 的链接,其中包含简单的操作方法,但代码无效:plnkr.co/edit/qFmpwQrNrOndVFORbZKX?
  • 我看到您在 plunker 中使用静态数据而不是服务 (http) 来删除您的个人资料,因此我制作了一个 plunker 来模拟它,因为您遇到了一些问题。这里没有实际的模态,但隐藏的 div 模拟了它。 plnkr.co/edit/p6ooBKvlHIaMFS4MuIAQ?p=preview
猜你喜欢
  • 2020-01-11
  • 2016-10-07
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
相关资源
最近更新 更多