Javascript 将表单输入发送到Angular 2中的方法
我制作了下面的Angular 2应用程序,它利用了数字API。我希望用户输入一个月中的一天和一年中的一个月,当他们单击按钮时,数据应该发送到getDayEvent()方法,然后该方法启动服务(使用虚拟数据)。我只需要找到一种方法将表单数据发送到方法。我对表单控件和表单组做了一些研究,但我很难在我的应用程序中实现它Javascript 将表单输入发送到Angular 2中的方法,javascript,angularjs,angular,angular2-forms,Javascript,Angularjs,Angular,Angular2 Forms,我制作了下面的Angular 2应用程序,它利用了数字API。我希望用户输入一个月中的一天和一年中的一个月,当他们单击按钮时,数据应该发送到getDayEvent()方法,然后该方法启动服务(使用虚拟数据)。我只需要找到一种方法将表单数据发送到方法。我对表单控件和表单组做了一些研究,但我很难在我的应用程序中实现它 @Component({ selector: 'http-test', template: ` <br> <form id="myForm" > <t
@Component({
selector: 'http-test',
template:
`
<br>
<form id="myForm" >
<table>
<tr>
<td><label>Enter a month</label></td>
<td><input type="text" ></td>
</tr>
<tr>
<td><label>Enter a day</label></td>
<td><input type="text"></td>
</tr>
<tr>
<td><button type="submit">See what happened on this day</button>
</td>
</tr>
</table>
</form>
<br>
<p id="output">{{event}}</p>
`,
providers: [HttpService],
directives: [CORE_DIRECTIVES, FORM_DIRECTIVES]
})
export class HttpComponent {
event: string;
constructor(private _httpService: HttpService){
}
getDayEvent(month:string, day:string){
this._httpService.getEvent(month, day)
.subscribe(
data => this.event = JSON.stringify(data),
error => alert(error),
() => console.log("Finished")
);
}
}
您可以在输入中使用ngModel,然后单击按钮中的事件 模板输入和按钮:
<input [(ngModel)]="month" type="text">
<input [(ngModel)]="day" type="text">
<button type="submit" (click)="getDayEvent(month,day)">See what happened on this day</button>
万分感谢!老实说,我没想到会那么容易。真的很感谢你的帮助,因为我对Angular 2还很陌生,所以我整天都在努力,现在我可以安心休息了。
<input [(ngModel)]="month" type="text">
<input [(ngModel)]="day" type="text">
<button type="submit" (click)="getDayEvent(month,day)">See what happened on this day</button>
export class HttpComponent {
event: string;
month: string;
day:string;