Answer the question
In order to leave comments, you need to log in
Angular Get request/form validation?
Hello!
There is a Get request
getStations(){
return this.http.get('http://62.141.52.96/site/act.php?action=getstations')
.map(
(response: Response) => {
const data = response.json();
return data;
}
);
}
ngOnInit(){
this.serverService.getStations()
.subscribe(
(data: any[]) => console.log(data),
(error) => console.log(error)
);
}
(3) [{…}, {…}, {…}]
0:
city: "Kiev"
id_city: "694"
1:
city: "Moscow"
id_city: "825"
2:
city: "New York"
id_city: "429"
for(var i=0;i<data.length;i++){
console.log (data[i].city)
};
Stations = ['Moscow', 'Kiev'];
ngOnInit() {
this.signupForm = new FormGroup({
'inputFrom' : new FormControl(null, [Validators.required, this.rightStations.bind(this)]),
}
rightStations (control: FormControl): {[s: string]: boolean}{
if (this.Stations.indexOf(control.value) ==-1){
return {'rightStationsIs': true};
}
return null;
}
Answer the question
In order to leave comments, you need to log in
1. There is a .map() method that iterates over the elements of an array:
ngOnInit(){
this.serverService.getStations()
.subscribe(
(data: any[]) => {
console.log(data.map((item) => item.city)
this.stations = data.map((item) => item.city)
}),
(error) => console.log(error)
);
}
rightStations (control: FormControl): {[s: string]: any}{
return this.Stations.indexOf(control.value) !== -1 ? {'rightStationsIs': true} : null
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question