Javascript Angular@Input属性-数组中缺少第一个值
我在将值从一个组件发送到另一个组件时遇到问题。我有两个组件:报告表单和评论表单。报告表单包含一系列注释。它还显示注释列表和每个注释的按钮。单击按钮后,它应加载带有@Input property commentIndex的模态表单。问题是报表表单组件并没有只将数组的第一个值(!)发送到注释表单。我不知道为什么:/ 下面是一些代码: report-form.html的一部分:Javascript Angular@Input属性-数组中缺少第一个值,javascript,angular,typescript,Javascript,Angular,Typescript,我在将值从一个组件发送到另一个组件时遇到问题。我有两个组件:报告表单和评论表单。报告表单包含一系列注释。它还显示注释列表和每个注释的按钮。单击按钮后,它应加载带有@Input property commentIndex的模态表单。问题是报表表单组件并没有只将数组的第一个值(!)发送到注释表单。我不知道为什么:/ 下面是一些代码: report-form.html的一部分: <div class="form-group row"> <div class="col-md-
<div class="form-group row">
<div class="col-md-12 alert alert-dark">Komentarze:</div>
<div class="col-md-12" *ngFor="let comment of comments">{{ comment.content }}
<app-comment-form [commentIndex]="comments.indexOf(comment)"></app-comment-form>
</div>
<br>
<app-comment-form></app-comment-form>
</div>
科门塔泽:
{{comment.content}
如果我以评论的形式登录控制台,我的3元素数组中有1,2个元素,但0在哪里 您可以直接使用
*ngFor
提供的索引
<div class="form-group row">
<div class="col-md-12 alert alert-dark">Komentarze:</div>
<div class="col-md-12" *ngFor="let comment of comments; let i = index">{{ comment.content }}
<app-comment-form [commentIndex]="i"></app-comment-form>
</div>
<br>
<app-comment-form></app-comment-form>
</div>
科门塔泽:
{{comment.content}
此
索引将从0开始。您确定您的注释数组在0
索引上有元素吗?在控制器中尝试console.log(this.comments[0])
。在这种情况下,您可以使用*ngFor中的索引*ngFor=“let comment of comments;let idx=index;”
是的,我确信我的数组在索引0处有元素,但这个技巧不起作用:(如果(this.commentIndex)
将失败,如果index
是0
,那么你可以发送[commentIndex]=“I+1”
是的,我自己找到了,但我遇到了另一个问题,因为我的值在模态中没有更新:/only first element of array work:/try-相同的结果可以创建一个工作的plunker。上面的代码似乎没有任何问题:(那么,你真正想要的是一个不工作的plunker?ba dum Tsst这将帮助我们调试发生的事情我很抱歉,这只是一个玩笑:)