0

I want to convert the jsonArray to a string in angular then show it as a select (dropdown picker) in html.

The JSON Url looks like this:

[
    "100 - BISCUIT - 546156",
    "252 - CHOCO - 185268",
    "131 - CANDY - 478215"
]

my.service.ts:

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs';

@Injectable()
export class MyService {

    //Json Url
    private jsonUrl = 'localhost:8080/jsonurl';

    constructor(private http: Http) {
    }

    public getContentFromApi() {
        return this.http.get(this.jsonUrl);
    }
 }

I have imported service in store.component.ts and in app.component file

import { Component, OnInit } from '@angular/core';
import { MyService } from '../../services/my.service'

@Component({
  selector: 'app-store',
  templateUrl: './store.component.html',
  styleUrls: ['./store.component.css'],
  providers: [MyService]
})

export class StoreComponent implements OnInit {
  selectedValue: any;
  storeItems: any;
  si: myService;



  constructor(){ }

  ngOnInit() {
    this.storeItems = this.si.getContentFromApi;
  }


  getStoreItems() {
    return this.storeItems;
   }


}

store.component.html

<div class="alignment">
  <p class="lev">Store Items: 
    <select [(ngModel)]="selectedValue"><option *ngFor="let s of storeitems" [ngValue]="s">{{storeitems}}</option></select></p>
</div>

As far as I can tell, my getcontentFromApi method is not right, and the binding to html select is not working.

anon
  • 855
  • 12
  • 22
Java Gamer
  • 567
  • 3
  • 9
  • 24
  • `this.si.getContentFromApi` is a method that returns async value (promise/observable). you need to resolve its value and set to `storeitems` (or store**I**tems). – dfsq Nov 15 '17 at 09:51
  • Afar as I understand I need to wait for the response so I get the data before moving to the next methd? I have seen examples where people use subscribe method, also how do i populate the select with the data? – Java Gamer Nov 15 '17 at 11:11
  • Set `this.storeItems` in subscribe. – dfsq Nov 15 '17 at 12:04
  • I found an another question which was more useful: https://stackoverflow.com/questions/34671715/angular2-http-get-map-subscribe-and-observable-pattern-basic-understan – Java Gamer Nov 15 '17 at 13:01

0 Answers0