i have a tasks.component.ts file in that the code is
addTask($event: Event) {
alert(this.name);
var newTask ={
name: this.name
}
//alert(this.tasks.push(newTask));
this.taskServices.addTask(newTask)
.subscribe(tasks => {
this.tasks.push(tasks);
});
}
task.services.ts this is my taskservices file..in that i write
addTask(newTask){
var headers = new Headers();
headers.append('ContentType','application/json');
return this.http.post('http://localhost:3000/edata',JSON.stringify(newTask)).map(res => res.json());
}
i am trying to pass the data 'this.tasks.push(tasks);' it should pass data to addTask(newTask){...} which is in task.services.ts
but it was not pushing the data to the addTask(){...} how to do that ,is there any wrong in my code?
You have to make newtask
variable as component level and need to provide provider and access that newTask method as shown below :
export class TasksComponent implements OnInit, AfterViewInit {
newTask;
constructor(private _taskService:taskService) { }
addTask($event: Event) {
alert(this.name);
this.newTask ={
name: this.name
}
this._taskService.addTask(this.newTask);
ngOnInit() {
this.taskServices.addTask(newTask)
.subscribe(tasks => {
this.tasks.push(tasks);
});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.