Restangular / Json-ld | I can get the data, but I can't show them

Issue

I’m new in Angular and need your help.
With my team, we made a PHP REST API in Symfony that we want to connect to an Angular client.

I already have the Angular app..
I can see in my XHR requests that i actually get the entities and their properties (users).

But in my view, i tried some {{ user.name }} with ng-repeat but it dosen’t show anything.

Datas are sent in Json-ld and we use Restangular to read them.

Here some code :
app.js :

    angular
  .module('frontApp', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch',
    'restangular'
  ])
  .config(function ($routeProvider) {
      $routeProvider
          .when('/', {
            templateUrl: 'views/main.html',
            controller: 'MainCtrl'
          })
          .when('/about', {
            templateUrl: 'views/about.html',
            controller: 'AboutCtrl'
          })
          .otherwise({
            redirectTo: '/'
          });
    })
    .config(['RestangularProvider', function (RestangularProvider) {
      // The URL of the API endpoint
      RestangularProvider.setBaseUrl('http://localhost:8000');

      // JSON-LD @id support
      RestangularProvider.setRestangularFields({
        id: '@id'
      });
      RestangularProvider.setSelfLinkAbsoluteUrl(false);

      RestangularProvider.setResponseExtractor(function(response) {
        var newResponse = response;
        if (angular.isArray(response)) {
          angular.forEach(newResponse, function(value, key) {
            newResponse[key].originalElement = angular.copy(value);
          });
        } else {
          newResponse.originalElement = angular.copy(response);
        }

        return newResponse;
      });

      // Hydra collections support
      RestangularProvider.addResponseInterceptor(function (data, operation) {
        // Remove trailing slash to make Restangular working
        function populateHref(data) {
          if (data['@id']) {
            data.href = data['@id'].substring(1);
          }
        }

        // Populate href property for the collection
        populateHref(data);

        if ('getList' === operation) {
          var collectionResponse = data['hydra:member'];
          collectionResponse.metadata = {};

          // Put metadata in a property of the collection
          angular.forEach(data, function (value, key) {
            if ('hydra:member' !== key) {
              collectionResponse.metadata[key] = value;
            }
          });

          // Populate href property for all elements of the collection
          angular.forEach(collectionResponse, function (value) {
            populateHref(value);
          });

          return collectionResponse;
        }

        return data;
      });
    }])
;

main.js :

angular.module('frontApp')
.controller('MainCtrl', function ($scope, Restangular) {

    $scope.ami1 = Restangular.one('amis',1).get();
    $scope.test ='Test';

    var amis = Restangular.all('amis');

      amis.getList().then(function (ami) {
          $scope.ami = ami;
      });
});

main.html:

<div class="jumbotron">
<p>Liste d'amis :</p>
{{ ami1.nom }}
{{ test }}
<div ng-repeat="ami in amis" id="{{ ami['@id'] }}" class="row marketing">
    <h1>{{ ami.nom }}</h1>
    <h2>{{ ami.prenom }}</h2>
    <h3>{{ ami.id }}</h3>
</div>

chrome console

Thanks for helping!

Solution

in your controller …

$scope.amis = Restangular.all('amis').getList();

if that doesn’t work …

Restangular.all('amis').getList().then(function(response) {
    $scope.amis=response;

}).catch(function(error) {

   console.log(error);
})

Answered By – danday74

Answer Checked By – Candace Johnson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.