0

I'm a little confused by how I should go about this. I have a "feed" in my app, where each post in the feed has a comment box. Here's some sample code:

<ion-card class="feed" *ngFor="let post of feed">
  <ion-item no-lines class="comment-input">
    <ion-input type="text" placeholder="Write comment ..."></ion-input>
    <button item-right ion-button (click)="feedPostComment(post)">Comment</button>
  </ion-item>
</ion-card>

Now what I'm struggling to figure out is the best way to go about feedPostComment() to pull the text from the input field above it. I know I could use ngModel and I have in many cases for forms and input systems on pages where they aren't repeating in this way, but I just can't wrap my head around how I would do it in this case.

I was thinking that I could set post.id to be the id or name field on the ion-input, and target the input field directly through the DOM, but I realize that isn't good practice.

The other thing is, the feed itself will be regularly updating with new posts. Sincerest thanks in advance.

1 Answer 1

1

Use ngModel with an object property to store comments added to that specific post. here is a stackblitz example.

<ion-card class="feed" *ngFor="let post of feed">
    <ion-item no-lines class="comment-input">
        <ion-input [(ngModel)]="post.comment" type="text" placeholder="Write comment ..."></ion-input>
        <button item-right ion-button (click)="feedPostComment(post)">Comment</button>
    </ion-item>
</ion-card>


//controller 
feedPostComment(post) {
  console.log('post_comment => ', post.comment);
  console.log('post_id => ',post.id);
}
Sign up to request clarification or add additional context in comments.

2 Comments

You are awesome. Thank you so much!
You are awesome Apu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.