passing multiple arguments or object in (click)

妖精的绣舞 提交于 2019-12-07 20:09:39

问题


Problem is to pass the object or multiple arguments from template to component and use them to add data to API.

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

addTasks(task){
this.taskService.addTasks(task)
.subscribe(
  task => this.tasks.push(task),
  error => this.errorMessage = <any> error
);

}

Template Inputs:

<input #todoTime type="text" class="form-control">&nbsp;
<input #todoName type="text" class="form-control">

Template Button:

<button name="todoAdd" (click)="addTasks({name: todoName.value, time: todoTime.value}); todoName.value='',todoTime.value='' ">add</button>

回答1:


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>

I have created this simple Plnkr that shows object is getting passed to addTasks() function.



来源:https://stackoverflow.com/questions/44574243/passing-multiple-arguments-or-object-in-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!