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

时间:2018-10-30 08:14:30

标签: html angular typescript

这是我简单的test.html文件,我想将employeeId传递给localhost:4200 / homePage。当我使用get方法时,它可以正常工作,但帖子出现错误,无法发布

log file

在homePage-component.ts文件中,我正在使用此代码-

var count = 0

describe('Main suite', function () {

  this.retries(5)

  it('Some setup', () => {
    console.log('1. Some setup');    
  });

  it("bail issue", function() {
    console.log('2. bail issue');
    if (count < 4) {
      count += 1
      throw new Error("Must be retried")
    }
  })

});

describe('end', function () {
  it('close', () => {
  });
});

1 个答案:

答案 0 :(得分:1)

基本上,您希望在提交后将数据从 page1 component发送到 page2 componentroute到Angular中的该组件的形式。

您应使用(ngSubmit)可用的Angular指令

尝试进行以下更改:

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

empId = "";

constructor(private router : Router) {} 

formSubmit(){
  this.router.navigate(['/home',{queryParameter : {employeeId : this.empId}}])
}

home.component.html

<div>{{employeeId}}</div>

home.component.ts

employeeId : String

constructor(private route : ActivatedRoute) {} 

ngOnInit(){
 this.route.queryParamters.subscribe(data=>{
   this.employeeId = data['employeeId']
 })
}

您还需要在应用程序中定义路由。 查看官方docs了解更多信息。