Reputation: 359
Hi i just want to use a simple function, http post to post my date to a page.I want the server be able to fetch the date i post.
import { Component, OnInit } from '@angular/core';
import { MarginServcies } from '../../services/MarginService';
import { Response } from '@angular/http';
import { Http} from '@angular/http';
//import { Ng2SmartTableModule, LocalDataSource } from 'ng2-smart-table';
@Component({
selector: 'margin',
template: require('./margin.component.html')
})
export class MarginComponent implements OnInit {
public http: Http;
public MarginList = [];
public date: string;
public userdate: string;
pad(i: number) {
return i < 10 ? '0' + i : i;
}
onClick($event: any, userdate) {
userdate = this.date;
//console.log(this.date);
this.http.post('api/date', userdate).subscribe();
}
//get the current date as default
ngOnInit() {
const selectedDate = new Date();
this.date = `${selectedDate.getFullYear()}-${this.pad(selectedDate.getMonth() + 1)}-${this.pad(selectedDate.getDate())}`;
}
public constructor(private marginService: MarginServcies) {
//get Margindata from server
this.marginService.getMargin().subscribe(results => {
this.MarginList = results.json();
//console.log(results.json());
});
}
}
This is my component. I am using webpack, angular 4. I want to use the post function inside the onClick() function. once I click i can post a date. Now i check the console.log() works fine. The erro is:
MarginComponent.html:15 ERROR TypeError: Cannot read property 'post' of undefined
at MarginComponent.onClick (margin.component.ts:25)
at Object.eval [as handleEvent] (MarginComponent.html:15)
at handleEvent (vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:22850)
at callWithDebugContext (vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:24142)
at Object.debugHandleEvent [as handleEvent] (vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:23730)
at dispatchEvent (vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:19750)
at vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:20342
at HTMLButtonElement.<anonymous> (vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:27870)
at ZoneDelegate.invokeTask (vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:83738)
at Object.onInvokeTask (vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:15077)
Could anyone help me with that? thanks!
Upvotes: 3
Views: 25250
Reputation: 7931
Http is an Inbuilt Angular Provider from the angular http module
import { Http, Response } from '@angular/http';
So you have to inject this http service in to the constructor. Then Only angular framework resolve the dependency
export class YourService {
constructor(private http: Http) {
}
Upvotes: 0
Reputation: 136144
Just by declaring public http: Http
will not make Http
provider instance available inside component. You have to inject http
service inside constructor, that will bring http
object.
public constructor(
private marginService: MarginServcies,
private http: Http //<-- added Http service.
) {
//your code as is
}
Remove the declaration of
public http: Http
(http declaration) from start of classpublic http: Http
Upvotes: 8