Search code examples
angularservicejsonresult

Consuming service response in Angular


In angular , I call an angular service which in return calls PHP which gets some data from MySQL and transforms the result into an associative array. This result I then send back to the angular's service class via PHP like this:

 echo json_encode($ret);

Note that $ret is an associative array. However, on the angular side, I am always getting an object. I want to get an array. I don't want to predict which properties exist in this object, I want the data on Angular side to be as an array, so that I can go through it using *ngFor . Any advice on how to get this service response as an array? Thanks for any help.


Solution

  • I think it is not possible to solve the issue without seeing your encoded data. If your data is object before encoding, it will become an object after Angular parses it.

    However, you can use KeyValuePipe of Angular to do this.

    <div *ngFor="let item of object | keyvalue">
      {{item.key}}:{{item.value}}
    </div>