[英]How to pass parameter from simple html page to another angular page using form post method?
this is my simple test.html file and i wanted to pass employeeId to localhost:4200/homePage.这是我的简单 test.html 文件,我想将 employeeId 传递给 localhost:4200/homePage。 When i use get method then it working fine but with post getting error Cannot Post
当我使用 get 方法时,它工作正常但发布错误无法发布
<form action="localhost:4200/homePage" name="goToAngular" method="post">
<input type="text" name="employeeID" value="" id="apiUrl" id="employeeID"/>
<input type="submit" value="go"/>
</form>
On the homePage-component.ts file i am using this code--在 homePage-component.ts 文件中,我使用了这个代码——
this.route.queryParams.subscribe((params: Params) =>
this.empID = params['employeeID'];
}
Basically you want to send data from page1 component
to page2 component
and route
to that component in Angular on submit of the form.基本上,您希望将数据从page1
component
发送到page2 component
并在提交表单时route
到 Angular 中的该组件。
You should make use of (ngSubmit)
directive available by Angular
您应该使用
Angular
提供的(ngSubmit)
指令
Try making following changes :尝试进行以下更改:
page1.component.html page1.component.html
<form (ngSubmit)="formSubmit()">
<input type="text" name="employeeID" value="" id="apiUrl" id="employeeID" [(ngModel)]="empId" />
<button type="submit">Submit</button>
</form>
page1.component.ts page1.component.ts
empId = "";
constructor(private router : Router) {}
formSubmit(){
this.router.navigate(['/home',{queryParameter : {employeeId : this.empId}}])
}
home.component.html主页.component.html
<div>{{employeeId}}</div>
home.component.ts home.component.ts
employeeId : String
constructor(private route : ActivatedRoute) {}
ngOnInit(){
this.route.queryParamters.subscribe(data=>{
this.employeeId = data['employeeId']
})
}
You will also need to define routes in your application.您还需要在应用程序中定义路由。 Check out official docs for more info.
查看官方文档了解更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.