首页 > 解决方案 > 如何使用角反应形式的字符串插值传递值

问题描述

我有一个帖子列表,每个帖子都有帖子评论表单,我正在尝试为每个帖子发送带有评论表单的帖子 ID,但它不起作用..

我试过用

  <form class="comment-form inline-items" [formGroup]="form2" (ngSubmit)="onSubmitComment()">

    <div class="post__author author vcard inline-items">
      <img src="{{user.photo}}" alt="author">

        <h1>{{post.id}}</h1> <!-- it's working here -->

      <div class="form-group with-icon-right ">
        <textarea class="form-control" placeholder="" name="body" formControlName="body"></textarea>

        <input  type="text"  value={{post.id}}  name="id" formControlName="id"/> <!-- it's not working here -->



      </div>
    </div>

    <button class="btn btn-md-2 btn-primary">Post Comment</button>


  </form>

我希望发送带有评论的帖子 ID,但它发送空值

标签: angularangular-reactive-forms

解决方案


这是我制作的Stackblitz 。您可以id在提交函数中看到正在传递的内容,并且(可选)输入字段中的更改反映在模型中。

让我知道这是否是你所追求的


推荐阅读