0

I was on a project with Angular and a backend.

If I enter a list in the ts file:

this.towns = [{
       code: '1',
       label: 'Paris'
   },
   {
       code: '0',
       label: 'Vancouver'
   }
 ];

I can display them in my html but not if I try to connect to a db:

<select  class="selectpicker dropdown mt-4 ml-3" id="town">
   <option *ngFor="let town of towns" [value]="town.id">{{town.name}}</option>
</select>

In my ts file, I tried:

export class TestComponent implements OnInit {
 
  form: FormGroup;
  towns: { id: number, name: string }[];
 
  constructor(
    private http: HttpClient
  ) { }
 
  towns: Town[];
  apiUrl = 'https://localhost:8000/api/';
 
  ngOnInit(): void {
    this.form = new FormGroup({
      town: new FormControl()
    });
    this.getTown();
  }
 
  onClickTown(): Observable<any> {
    console.log(this.form)
    return this.http.get(this.apiUrl + 'towns')
  }
 
  getTown() {
    this.http.get<any[]>(this.apiUrl + 'towns')
      .subscribe(data => {
        this.towns = data;
        console.log(data)
      });
  }
 
  postData() {
    console.log("data");
    this.http.post<any[]>(this.apiUrl, {}).
      subscribe(
        (data: any[]) => {
          // console.log(data);
        }
      )
  }

Did I forget a step? Thanks

3
  • Why do you declare the towns property two times?! towns: { id: number, name: string }[]; and towns: Town[]; Commented May 31, 2021 at 6:59
  • @ArmenStepanyan, getData?I have a getTown and in my console it shows: link like this. Commented May 31, 2021 at 7:37
  • @RamiAssi, that might be because I tried many things and forgot to take the wrong ones down. Thanks. I missed it Commented May 31, 2021 at 7:38

3 Answers 3

2

This is because changes are not detected. You can try reinitializing the towns array so that Angular detects changes.

getTown() {
  this.http.get<any[]>(this.apiUrl + 'towns')
    .subscribe(data => {
      /* reinitialize the towns array */
      this.towns = [];
      /****/
      this.towns = data;
      console.log(data)
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried your solution and didn't get progress but noticed a new error "Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays." that apparently could be solved if I put "| async" in my ngFor but didn't.
This means that the returned data is apparently not an array. The problem is definitely not in the answer. Please include the output of console.log(data) in your question
The returned data shown in your screenshot is not an array. It is an object. Make sure that you are calling the right API method or you might need to provide extra parameters.
1

Try the code below.

I've used the async pipe to iterate. (Notice you should return Observable for that) I've also used the ngValue attribute to bind to the form

HTML

<select formControlName="town">
  <option *ngFor="let town of towns | async" [ngValue]="town">{{town.name}}</option>
</select>

TS interface

exprot interface Town{
  id:number;
  name:string;
}

TS Component

export class TestComponent implements OnInit {

  form: FormGroup;
  towns:Observable<Town[]>;
  apiUrl:string = 'https://localhost:8000/api/';

  constructor(private http: HttpClient) { }

  ngOnInit(): void {        
    this.towns = this.http.get<Town[]>(this.apiUrl + 'towns');        
    this.form = new FormGroup({
      town: new FormControl()
    });
  }

1 Comment

thanks. I tried the |async and changed on my getTown function:
0

I changed my getTown function to solve it and so my html:

  getTown() {
    this.townsObs = this.http.get<any[]>(this.apiUrl + 'towns').subscribe(data => {
        this.towns = data['hydra:member'];
        console.log(data['hydra:member'])
      });
  }

Thanks to you all

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.