Reputation: 127
I want to display individual value of json object into form in angular. How to do that?
output
{
"Order": {
"active": true,
"Orders_Entrydate": "2017-12-31T18:30:00.000Z",
"LastTouchDateTime": "2018-05-10T05:46:38.702Z",
"_id": "5af07544eb26f918e0e2ff74",
"Orders_Name": "Test1",
"Orders_Number": "1011",
"Orders_LoanNumber": 1328,
"Orders_PropertyAddress1": "test address1",
"Orders_PropertyAddress2": "test address 1",
"Orders_PropertyCity": "testCity",
"Orders_Propertyzipecode": 1236,
"Orders_Countyfee": 500,
"Orders_Additionalfee": 100,
"Orders_Customerpricing": 150
}
}
view-order.ts
export class countyViewOrderComponent implements OnInit {
orders: any[];
constructor(private orderService: countyvieworder, private router: Router, private cookie_service: CookieService) {
}
getorder() {
this.orderService.getOrders().subscribe(response => {
this.orders = response.json();
})
}
onSelect(selectedItem: any) {
this.cookie_service.put('key', selectedItem._id)
// this.cookie_service.get('key')
this.router.navigate(['pages/home/County/orderEntrybyCounty']);
}
ngOnInit() {
this.getorder()
}
}
getOrderbyOrderNo() service
getOrderbyOrderNo() {
this.id = this.cookie_service.get('key');
return this.http.get('http://localhost:8080/View/'+ this.id)
}
Getting this error:
Argument of type 'Response' is not assignable to parameter of type 'string'.
Upvotes: 3
Views: 151
Reputation: 176896
Suggest you to make use of HttpClient
, that will do conversation for you . HTTPClient is new module in angular which replaced old HTTP one.
import HttpClientmodule
in your module
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
HttpClientModule,
],
change in service.ts
import { HttpClient } from '@angular/common/http';
constructor(private httpClient: HttpClient) { }
getOrderbyOrderNo() :Observable<any> {
this.id = this.cookie_service.get('key');
return this.httpClient.get('http://localhost:8080/View/'+ this.id)
}
view-order.ts
order: any;//no array as you are expecting only one element
getorder() {
this.orderService.getOrders().subscribe(response => {
this.order = response;
})
}
in your code you are using array that is also cause problem
Upvotes: 1
Reputation: 79
You are not parsing your response so here this code may be help you
this.orders=JSON.parse(response);
or you can also do this in your service
return this.http.get('http://localhost:8080/View/'+ this.id).map(data => data.json());
Upvotes: 0
Reputation: 18647
Change the getorder
function subscribe
to subscribe((response: any))
getorder() {
this.orderService.getOrderbyOrderNo().subscribe((response: any) => {
console.log(response);
this.orders=response
})
}
Upvotes: 1