gpt4 book ai didi

javascript - 如何在文件来自 JSON 的 Angular 中编辑和更新数组项

转载 作者:行者123 更新时间:2023-11-30 19:07:50 25 4
gpt4 key购买 nike

我正在尝试执行一个编辑,该编辑将编辑对象,然后在更新时将添加新数据并删除旧数据。我只想编辑和更新 comments$

我得到了 JSON 我正在显示我可以编辑但我无法保存并在 UI 中显示。

这是我试过的代码。

@Injectable({
providedIn: 'root'
})
export class CommentService {
private baseUrl = 'http://localhost:3000/comments';

constructor(private http: HttpClient) { }

getComment(id: number): Observable<any> {
return this.http.get(`${this.baseUrl}/${id}`);
}
update(comment: Comment, id) {
return this.http.put(`${this.baseUrl}/${id}`, comment);

}
}

posts$: Observable<Post[]>;
comments$: Observable<Comment[]>;
groupedComments$: Observable<CommentGroup>;
editableComment = emptyComment();

constructor(private postsService: PostService,
private commentsService: CommentService,
private confirmationDialogService: ConfirmationDialogService) {
this.posts$ = this.postsService.getPostsList();
this.comments$ = this.commentsService.getCommentsList();
this.getAllData();
}

ngOnInit() {

}
getAllData() {
this.groupedComments$ = this.comments$.pipe(
map(comments => lodash.groupBy(comments, 'postId'))
);
}

isEditable(comment: Comment): boolean {
return this.editableComment.id === comment.id;
}
editComment(comment: Comment) {
this.editableComment = comment;
this.getAllData();
}
update(itemId, itemName, itemObj) {
console.log(itemId, itemName, itemObj);
}
cancel() {
this.editableComment = emptyComment();
}

这是 HTML

<tr *ngFor="let post of posts$ | async; trackBy:trackByFunction">
<td class="title">{{post.title}}</td>
<td class="body">{{post.body}}</td>
<ng-container *ngIf="groupedComments$ | async as groupedComments">
<div *ngFor="let comment of groupedComments[post.id]; trackBy:trackByFunction">
<div>
<td *ngIf="!isEditable(comment)" class="comment">{{comment.name}}</td>
<textarea class="comment" *ngIf="isEditable(comment)" [(ngModel)]="editableComment.name"></textarea>
<td *ngIf="!isEditable(comment)"class="comment">{{comment?.body}}</td>
<textarea class="comment" *ngIf="isEditable(comment)" [(ngModel)]="editableComment.body"></textarea>

<td class="comment" *ngIf="comment.email === 'Just@do.it' && comment.body.length < 200">
{{comment.email}}
<button *ngIf="!isEditable(comment)" (click)="deleteComment(comment.id)" class="btn btn-danger">Delete</button>
<button *ngIf="!isEditable(comment)" (click)="editComment(comment)" class="btn btn-info" style="margin-left: 10px">Edit</button>
<button *ngIf="isEditable(comment)" (click)="update(comment.id, comment.name, comment)" class="btn btn-info" style="margin-left: 10px">Update</button>
<button *ngIf="isEditable(comment)" (click)="cancel()" class="btn btn-danger" style="margin-left: 10px">Cancel</button>
</td>
</div>
</div>
</ng-container>
</tr>

最佳答案

更新了重要部分。您需要在某个地方使用一个函数来存储更改后的评论。

groupedComments$: Subject<CommentGroup> = new ReplaySubject(1);

getAllData() {
this.commentService.getCommentsList()
.pipe(map(comments => lodash.groupBy(comments, 'postId')))
.pipe(take(1)) // this will unsubscribe for you, after one value was emitted
.subscribe(commentGroup => this.groupedComments$.next(commentGroup));
}

editComment(Comment comment) {
this.commentService.update(comment);
getAllData();
}

在 HTML 上,您可以订阅 this.groupedComments

关于javascript - 如何在文件来自 JSON 的 Angular 中编辑和更新数组项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58800506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com