5

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>
2
  • right now what problem you are facing? Question statement does not explain the error or problem. Commented Jun 15, 2017 at 22:44
  • Also I think this is possible "<button (click)="addTask(todoName)"></button>" Commented Jun 15, 2017 at 22:46

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.