I am trying to create a component to show Data.
In my Parent Component
I am creating a config obj which will be passed to the ItemViewer Component
export class ProcessListComponent implements OnInit {
process_list: any;
configObj: {};
constructor(public http: HttpClient){}
ngOnInit() {
// this is the config Obj
this.configObj = {
data : this.getAll,
fields : {
title : 'Id',
body :[
{ name : 'Name' , field : 'name'},
{ name: 'Final Material', field : 'final_material'},
{ name : 'Rivision' , field : 'revision'}
]
},
selectionChanged : this.selectionChangeDetection
}
}
selectionChangeDetection(item){
alert(item)
// some events here, some actions here related to visiblity
}
// function to fetch Obj List
getAll(option: string) {
// console.log(this)
return (function(extra_option: string){
const url: string = 'http://localhost:3000/process_list' + extra_option;
return this.http.get(url); // <-- Error comes at this line. How to handle it ??
})(option);
}
}
I pass this obj to ItemViewer Component
and handles it as below:
export class ItemViewerComponent implements OnInit {
@Input() config;
constructor(private http: HttpClient) { }
ngOnInit() {
this.config.selectionChanged('it works'); // <-- This function works
this.config.data()
.subscribe(data => console.log(data)); // <-- Error here
}
}
Now, when I run this I get
ProcessListComponent.html:1 ERROR TypeError: Cannot read property 'http' of undefined at process.list.component.ts:40 at Object.webpackJsonp.../../../../../src/app/features/process/list/process.list.component.ts.ProcessListComponent.getAll [as data] (process.list.component.ts:41) at ItemViewerComponent .webpackJsonp.../../../../../src/app/common/ICV/icv.component.ts.ItemViewerComponent .ngOnInit (item-viewer.component.ts:14) at checkAndUpdateDirectiveInline (core.es5.js:10843) at checkAndUpdateNodeInline (core.es5.js:12341) at checkAndUpdateNode (core.es5.js:12284) at debugCheckAndUpdateNode (core.es5.js:13141) at debugCheckDirectivesFn (core.es5.js:13082) at Object.eval [as updateDirectives] (ProcessListComponent.html:1) at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:13067)
Now, I know that I am getting this error because this
does not have http
var of ProcessListComponent
.
The same config used to work with $http
when I had the application in Angular1.X and I used to pass
function getAll(option) {
return (function (ur_option){
return $http.get('http://XYZ&units=metric&'+ur_option)
})(option)
}
Update
Thanks to ConnorsFan& Radonirina Maminiaina: below code worked
getAll = (option: string) => {
return ((extra_option: string) => {
const url: string = 'http://localhost:3000/process_list' + extra_option;
return this.http.get(url);
})(option);
}