I'm making an application with Angular 4 and using Materialize CSS. My goal is: when a certain component is loaded, an HTTP query is made that extracts some data, which will dynamically fill a select content in the view. Next, I show the service that makes the query:
import { Injectable } from '@angular/core';
import { Http, Response, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
import { Observable} from 'rxjs/Observable';
@Injectable()
export class citasService{
public url:string;
constructor(private _http:Http){
this.url = "URL API";
}
getCenTra(){
return this._http.get(this.url)
.map(res => res.json());
}
}
This service is injected into the component, and the search is performed successfully with the method ngOnInit()
. The code is shown below:
import { Component } from '@angular/core';
import { citasService } from '../services/citas.service';
import { CitasModel } from './citas.model';
declare var $:any;
declare var Jquery:any;
@Component({
selector: 'citas',
templateUrl: './citas.component.html',
styleUrls: ['./citas.component.css'],
providers: [ citasService ]
})
export class citasComponent {
public datos_Cen_Tra: CitasModel;
constructor(private _resultado:citasService){
this.datos_Cen_Tra = new CitasModel("",0,"","",0,"", "");
}
ngOnInit(){
$('select').material_select('destroy');
this._resultado.getCenTra().subscribe(
result=>{
if(result.count == 1){
this.datos_Cen_Tra = result.cen_tra[0];
$('select').material_select();
}else{
console.log(result);
}
},
error=>{
var errorMessage = <any>error;
console.log(errorMessage);
}
);
}
And then the html where the result is printed:
<div class="input-field col s12">
<select id="listCenTra">
<option value="">{{datos_Cen_Tra.name_com}}</option>
</select>
<label>Seleccione centro de trabajo</label>
</div>
The problem is that, the data does not get loaded in the select:
On the other hand, if I print it on the screen, for example inside a p-label, if it is printed correctly:
<p>{{datos_Cen_Tra.id}}</p>
I do not know what may be failing. Thank you in advance for the help.