在(单击)中传递多个参数或对象 [英] passing multiple arguments or object in (click)
本文介绍了在(单击)中传递多个参数或对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题是将对象或多个参数从模板传递到组件,并使用它们将数据添加到API.
Problem is to pass the object or multiple arguments from template to component and use them to add data to API.
task.service.ts
task.service.ts
addTasks(task: Task): Observable<Task>{
let headers = new Headers({'Content-type': 'application/json'});
let options = new RequestOptions({ headers: headers });
return this.http.post(this.tasksUrl, {task}, options)
.map(this.extractData)
.catch(this.handleError);
}
task.component.ts
task.component.ts
addTasks(task){
this.taskService.addTasks(task)
.subscribe(
task => this.tasks.push(task),
error => this.errorMessage = <any> error
);
}
模板输入:
<input #todoTime type="text" class="form-control">
<input #todoName type="text" class="form-control">
模板按钮:
<button name="todoAdd" (click)="addTasks({name: todoName.value, time: todoTime.value}); todoName.value='',todoTime.value='' ">add</button>
推荐答案
在处理按钮的click
事件时,用分号替换comman(,).那应该起作用.
Replace the comman(,) with a semicolon when you are handling the click
event for the button. That should work.
<button name="todoAdd" (click)="addTasks({name: todoName.value, time: todoTime.value}); todoName.value=''; todoTime.value='' ">add</button>
我已经创建了一个简单的 Plnkr ,该对象显示对象已传递给addTasks()
功能.
I have created this simple Plnkr that shows object is getting passed to addTasks()
function.
这篇关于在(单击)中传递多个参数或对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文