[英]Simple edit functionality is not working in angular js using routes?
[英]To get Id for edit functionality in angular
我在待办事项应用程序中创建了一个用于编辑功能的弹出窗口,为此我需要获取任务的 ID。 使用路线没有给出正确的结果。 还有其他方法可以实现吗? 我已经给出了下面的代码。
this.id = this.route.snapshot.params['id'];
console.log(this.formData.value);
this.todoService.editTasks(this.id, this.formData.value).subscribe((res: any)=>{
console.log('update successful');
// this.router.navigateByUrl('/tasks');
})
}```
确认您在编辑组件的路径中具有相同的 ID:
{path: "edit/:id", component: someComponent}
在组件中使用它
import { ActivatedRoute } from '@angular/router';
constructor(private route: ActivatedRoute) {}
ngOnInit(){
//you need to unsubscribe in the ngOnDestroy
this.subscription = this.route.paramMap.subscribe(params=> {
//i use + to convert the id to a number type
let id =+params.get('id');
}
//To prevent memory leak
ngOnDestroy(): void {
if (this.subscription)
this.subscription.unsubscribe()
}
或使用:
ngOnInit(){
let id =this.route.snapshot.paramMap.get('id');
}
使用 Angular 文档: https://angular.io/tutorial/toh-pt5#extract-the-id-route-parameter
您需要使用ActivatedRoute才能从 URL 获取 id,请按照以下步骤使用它。
// import
import { ActivatedRoute } from '@angular/router';
//dependency injection
constructor(private route: ActivatedRoute) { }
//implementation
ngOnInit() {
this.route.paramMap.subscribe(
params => {
this.id= params.get('id'); // This is the required Id
})
}
我希望这能回答你的问题,如果我有什么不对,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.