【问题标题】:ngFor alway returning id of first object in arrayngFor 总是返回数组中第一个对象的 id
【发布时间】:2018-11-23 21:03:17
【问题描述】:

我试图通过在单击按钮时将对象分配给组件中声明的变量来获取对象的 ID。但是,它似乎每次只获取第一个对象 ID。

这是 StackBlitz 的链接,它复制了我遇到的问题:

https://stackblitz.com/edit/angular-vcbzxf

任何帮助将不胜感激。

【问题讨论】:

  • 我认为是bootstrap-model 让这个怪癖发生了。我自己是 Angular 新手,但我在这个 stackblitz 上做了一些事情,希望这能帮助你找到正确的方向 - stackblitz.com/edit/angular-ydtpdq 请注意,对话框上的 delete 按钮没有给出预期的结果,但 delete评论部分下方的按钮可能会记录预期的 id。
  • 是的,它肯定与模态有关。由于某种原因,模态无法查看变量的值。这是一件如此简单的事情,但也如此令人沮丧!感谢您的评论
  • 我建议看一下其他 SO 帖子,可能是 google 'angular bootstrap modal',我无法很好地搜索这个,因为我目前无法访问计算机,但是一旦我可以访问电脑就会这样做。
  • 谢谢,但我决定采用一种不需要模式的不同 UX 方法。感谢您的努力:)
  • 出于好奇,您打算集成任何其他库吗?如果您对集成第三方库的想法持开放态度,我强烈建议您查找“devextreme”,devextreme 的弹出窗口与引导模式非常相似,并且非常可配置。

标签: angular5 bootstrap-modal variable-assignment ngfor


【解决方案1】:

更新 1 :您实际上可以使用引导模式实现您想要的,只需进行一些更改,如下所示。

comment.component.html:

<div *ngFor="let comment of post.comments; let i = index">

  {{ comment.content }}
  <button data-toggle="modal" [attr.data-target]="'#confirmDeleteCommentModal' + comment.id">Get ID</button>

    <div class="fade modal" [attr.id]="'confirmDeleteCommentModal' + comment.id" tabindex="-1" role="dialog" aria-labelledby="confirmDeleteCommentModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="confirmDeleteCommentModalLabel">Delete Comment</h5>
                    <button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>
                    <button type="button" class="btn btn-primary" (click)="checkId(comment.id)">Delete</button>
                </div>
            </div>
        </div>
    </div>

</div>

注意 [attr.data-target]="'#confirmDeleteCommentModal' + comment.id" 添加到 Get ID 按钮和 [attr.id]="'confirmDeleteCommentModal' + comment.id" 添加到引导模式。此外,模态现在包含在*ngFor div element中。

以前,Get ID 按钮的 data-target 始终设置为 confirmDeleteCommentModal,从技术上讲,我们只有一个引导模式,id 设置为 confirmDeleteCommentModal,我认为这是导致问题的原因,我们总是加载相同(也是第一个)引导模式。

更新堆栈闪电战:https://stackblitz.com/edit/angular-3tmtwa


原始答案(这并没有解决问题):

如果您按照您最近的评论(在此处实现 - https://stackblitz.com/edit/angular-vcbzxf),我想指出您可以按如下方式编写您的 comment.component.html

comment.component.html

<div *ngFor="let comment of post.comments">
    <div>
        {{showDelete ? 'Are you sure you want to delete?' : comment.content + ' id = ' + comment.id}}
        <button *ngIf="showDelete" (click)="showDelete = false">Cancel</button>
        <button (click)="(showDelete == false && showDelete = true) || checkId(comment.id)">Delete</button>
    </div>
</div>

Stackblitz 给它一个快速测试:https://stackblitz.com/edit/angular-q27xn7

虽然这可能与您所做的相比没有很大的改进,但我想我只是指出实现类似结果的其他方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-18
    • 2018-12-08
    • 2014-04-14
    • 1970-01-01
    • 2015-09-05
    • 2021-06-06
    • 2021-08-15
    • 2020-03-30
    相关资源
    最近更新 更多