How to bind dynamic values to the dropdown using ionic?


#1

Hi im just trying to bind the dynamic values to the dropdown but i confused how to bind it .

here is my json data

   [
  {
    "Name": "data1",  
    "sno": 3

  },
  {
    "Name": "data2",  
    "sno": 4
  },
  {
    "Name": "data3",  
    "sno": 5
  }

here im calling my dynamic data using providers

dropDown(){
var url1 ='http://xxxxx.xxxx.xxxx.xxx/info/Service1.svc/getData';
return this.http.get(url1).map(result=>{
  return result.json();
})

after that im calling this in a page by calling it in the constructor and then using ngOnIt im initializing it

selectedValue: number;
  optionsList: Array<{ value: number, text: string}> = [];

the after declaring in the constructor

`ngOnInit(){



    this.optionsList = this.serv.dropDown().subscribe( response =>{

      console.log(response.json());

    });

  }`
<ion-select>
    <ion-option *ngFor="let item of optionsList" value="{{item.value}}">{{item.text}}</ion-option>
  </ion-select>

but im getting error as subscription is not assignable to the type and how to get the selected value and text?


#2

Because you bothered to give things types, you got an error message that guides you to what is wrong. The return value of subscribe is a subscription; what you probably want to do instead of assigning it to optionsList is to assign inside the subscription:

this.serv.dropDown().subscribe(opts => this.optionsList = opts);