简体   繁体   English

如何使用表单发布方法将参数从简单的 html 页面传递到另一个角度页面?

[英]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.

相关问题 如何将参数从 html 页面传递到 angular 自定义元素 - How to pass a parameter from html page to angular custom element 通过HTML页面作为方法参数 - Pass Html page as method parameter 如何使用 AJAX 将参数从 1 个 HTML 页面传递到另一个 HTML 页面? - How can I pass parameter from 1 HTML page to another HTML page with AJAX? HTML页面上的表单POST方法 - Form POST method on html page 如何使用CGI使用POST从HTML页面重定向到另一个页面? - How to use a CGI to redirect from an HTML page to another using POST? 使用<form>如何在不打开页面的情况下将数据传递到另一个 html 页面</form> - Using <form> how would I pass data to another html page without opening the page 如何从HTML表单发布到Facebook页面 - How to post to a facebook page from an html form 如何使用javascript将值从一个html页面传递到另一个html页面 - How can I pass values from one html page to another html page using javascript 如何使用另一个Wordpress帖子的永久链接以编程方式创建一个简单的HTML页面? - How to programatically create a simple HTML page using the permalink of another Wordpress post? 如何将变量从 php 页面传递并放入另一个页面(表单)? - How to pass and put variable from a php page into a another page(form)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM