我在一個網站上作業(基本上是一個 twitter 克隆),我想在帖子中添加評論。為此,我必須將推文的 ID 和評論本身傳遞給后端。到目前為止,我有以下代碼:
toggle(event: Event): void {
let elementId: string = (event.target as Element).tagName;
console.log(elementId);
this.commentModel.value.ID = elementID;
}
這在 HTML 中被呼叫如下:
<form [formGroup]="homepageservice.commentModel"
class="container d-flex row rounded-pill p-2 justify-content-between align-items-center w-80 addCommentBox"
id="add-comment" (submit)="onSendComment()">
<textarea type="text" name="comment" class="rounded-pill col" id="comment-input"
formControlName="commentcontent" placeholder="Write comment here...."></textarea>
<button type="submit" name={{tweet.ID}} id="addComment" class="btn btn-green rounded-pill"
(onclick)="homepageservice.toggle($event)">
<i class="bi bi-send-fill"></i>
</button>
</form>
最后是表單和發送功能:
commentModel = this.fb.group({
commentcontent: [''],
id: [''] })
sendComment() {
const commentText = this.commentModel.value.commentcontent;
const id = this.commentModel.value.ID;
return this.http.post(this.apiUrl `/Tweets/newComment?
comment=${commentText}&tweetID=${id}`, {}); }
當我運行這些時,我得到一個未定義的 ID,并且無法找出問題所在。
uj5u.com熱心網友回復:
你可以這樣做:(click)="homepageservice.toggle(tweet)"
在你的服務中:
toggle(tweet: any) {
console.log(tweet);
this.commentModel.value.ID = tweet.ID;
}
uj5u.com熱心網友回復:
你為什么不嘗試像這樣傳遞一個 ID 引數?
<button type="submit" name={{tweet.ID}} id="addComment" class="btn btn-green rounded-pill"
(onclick)="sendComment(tweet.ID, $event)">
<i class="bi bi-send-fill"></i>
</button>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/480766.html
上一篇:如何將現有代碼實作為函式?
