【发布时间】:2021-12-22 12:26:36
【问题描述】:
我有以下代码:
HTML:
<input
#title
(keyup.enter)="createPost(title)"
type="text"
class="form-control">
<ul class="list-group">
<li *ngFor="let post of posts" class="list-group-item">
{{ post.title }}
<button class="btn btn-default btn-sm" (click)="updatePost(post)">Update</button>
</li>
</ul>
角度:
export class PostsComponent implements OnInit {
posts: any;
url = 'https://jsonplaceholder.typicode.com/posts'
constructor(private http: HttpClient) {
http.get(this.url)
.subscribe(response => {
this.posts = response;
})
}
----
updatePost(post: any) {
this.http.put(this.url + '/' + post.id, JSON.stringify({ title: 'Example' }))
.subscribe(response => {
})
}
如何使用标题“示例”更新给定 ID 的帖子值? 如果我 console.log(response) 它只返回一个 ID,而不是一个正确的值。 请帮忙:)
【问题讨论】:
标签: javascript html angular frontend