Im a newbie to angular.. Given below is a test code which Im writing.. Summary : My problem is that Im not getting a value back into an object, eventhough that Im able to see that in the log. Detail : Im using Angular4, given below is my component code , service and html code, My intention is that when I hit the submit button , the subscriber will call a service using "this.pythTicker" as a parm to a REST API service and the service will return some values for "this.pythTicker" into the property retPythService. The returned values are getting printed in log properly but not getting assigned to this.retPythService. SO my question is, Am I doing something wrong in populationg this.retPythService?? The confusion I have is that, the printing to the log just above that works but the subscribing to this.companyRet.compname does not work My eventual plan is to use the property "retPythService" to populate the second line of the HTML(this.companyRet.compname ).
HTML##### pythsearch.component.html<form (submit)="onPythSubmit()">
<div class="form-group">
<label>Name</label>
restApiSearch-Ticker: <input type="text" [(ngModel)]="pythTicker" name="Ticker"> <input type="submit">
</div>
</form>
<h3> ticker details from a Pyth RESTAPI {{ this.companyRet.compname }} </h3>
COMPONENT####### pythsearch.component.ts
import { PythSrchService } from '../../services/PythSrchService.service';
@NgModule({
declarations: [
PythsearchComponent
],
imports: [BrowserModule, FormsModule],
providers: [ PythsearchComponent ],
bootstrap: [ PythsearchComponent ],
})
export class PythsearchComponent implements OnInit {
pythTicker: any;
companyRet = {
bond : '',
compname : '',
convexity : '',
price : '',
};
retPythService: any[];
errMsg: string;
constructor( public PythSrchInst: PythSrchService ) { }
ngOnInit() { this.retPythService = '';}
onPythSubmit() {
console.log ('hit onsubmit', this.pythTicker);
// try 1 WORKS !!!!, Im passing pythticker and subscribing to the REST API service
this.PythSrchInst.getTickerDetails(this.pythTicker).subscribe(
retPythService => console.log('retpthlog', retPythService) );
// try 2 does not work, trying to move it to another object this.retPythService
this.PythSrchInst.getTickerDetails(this.pythTicker).subscribe(
retPythService => {this.retPythService = retPythService ; } );
// the below statements return empty values in LOG
console.log('thisretpythservice in component :' , this.retPythService);
console.log('compantret.compname:' , this.companyRet );
}
}
The service fetching the RESTAPI data
export class PythSrchService {
PythApi= 'http://127.0.0.1:5002/PythSearch';
retPythService: any;
constructor( public http: Http ) {}
getTickerDetails(PassTicker) {
this.retPythService = `${this.PythApi}\/${PassTicker} `;
console.log('api call:', this.retPythService);
// console.log( 'service call:' , this.http.get(this.retPythService).map(res => res.json()));
return this.http.get(this.retPythService).map(res => res.json());
}
LOG OUTPUT
api call: http://127.0.0.1:5002/PythSearch/TSLA
thisretpythservice in component : undefined
compantret.compname: -->>> empty
Object { bond: "", compname: "", convexity: "", price: "" } -->> empty
retpthlog
Object { bond: "Testla Motor corp 10 year bond", compname: "tesla motor corp", convexity: "0.52", price: "102" } >>> Not empty , why?