0

I am trying to display info in AngularJS as

<div ng-repeat="track in list.tracks" class="trackInfo">
            <span id="trackName" style="font-size: 32px;">{{ track.title }}</span>
</div>

console.log of list.tracks is as follows:

(20) [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]

Each object contains values like

0:Object
genres: Array(1)
id: 84
title: "name"

The output isn't being displayed. It doesn't show anything. Fiddle

5
  • 1
    can you try console.dir(list.tracks) Commented May 10, 2017 at 17:49
  • Array(20)0: Objectgenres: Array(1)0: Objectlength: 1__proto__: Array(0)id: 84title: "name"__proto__: Object1: Objectgenres: Array(0)id: 85title: "name2"__proto__: Object2: Object3: Object4: Object5: Object6: Object7: Object8: Object9: Object10: Object11: Object12: Object13: Object14: Object15: Object16: Object17: Object18: Object19: Objectgenres: Array(0)id: 138title: "Adele"__proto__: Objectlength: 20__proto__: Array(0) Commented May 10, 2017 at 17:52
  • How you need to display the result, in the html view or in console?. Please create a jsfiddle and share Commented May 10, 2017 at 17:54
  • in html view fiddle Commented May 10, 2017 at 18:05
  • i have shared an answer. Please check Commented May 10, 2017 at 18:50

3 Answers 3

1

Try console.log(JSON.stringify(list.tracks));

Sign up to request clarification or add additional context in comments.

Comments

0

I made a directive to log object or array with angular

Set $rootScope.debug = true to make it displayed ! ( or remove the ng-if="$root.debug" in the view )

Usage :

<dir.log dlog="test" dtitle="test from controller"></dir.log>

In your controller

$scope.test = { genres: ["male"],id: 84,title: "name" }

The directive code

var directives = angular.module('directives', []);
directives.directive("dir.log", function () {
   return {
      restrict: 'E',
      replace: false,
      scope: {
         dlog: '=', // what object or array to log
         dtitle: '@' // the title displayed on hover of the show/hide button

      },
      controller: function ($scope, $rootScope) {
         $scope.logshow = false ;       
      },
      templateUrl: "partials/elements/log.html"
   }
});

the html code

<div ng-if="$root.debug" class="text-left">     
        <button tooltip-placement="right" uib-tooltip="log debug {{dtitle}}" ng-hide="logshow" ng-click="logshow=true;" class="btn btn-default btn-xs" type="button">
            <span class="glyphicon glyphicon-collapse-down"></span></button>
        <button tooltip-placement="right" uib-tooltip="log debug {{dtitle}}" ng-hide="!logshow" ng-click="logshow=false;" class="btn btn-default btn-xs ng-hide" type="button">
            <span class="glyphicon glyphicon-collapse-up"></span>
        </button>
        <pre ng-if="logshow"><span ng-show="dtitle!=null">{{dtitle}}:<br></span>{{dlog |json}}</pre>

</div>

Comments

0

I have used $scope and its working in this way.

html

<div ng-app="ListTracksApp" ng-controller="MusicController">
      <h3>Music Tracks</h3>
          <div ng-repeat="track in tracks" class="trackInfo">
                <span class="trackName" style="font-size: 32px;">{{track.title}}</span>
                <div class="rating">{{ track.rating }}</div>

        </div>
    </div>

angular file

(function () {
var myApp = angular.module('ListTracksApp', []);
myApp.controller('MusicController', MusicController);
myApp.service('MusicService', MusicService);
MusicController.$inject = ['MusicService','$scope'];

function MusicController(MusicService,$scope){
  var vm = $scope;
  vm.name="test";
  var promise = MusicService.getTracks();
  //console.log(promise);
  promise.then(function(response) {
    vm.tracks = response.data.results;
    console.log(vm.tracks);
    //console.dir(list.tracks);
  })
  .catch(function(error){
    console.log("Something went wrong!");
  });

}


MusicService.$inject = ['$http'];
function MusicService($http) {
  var service = this;
  service.getTracks = function(){
    var response = $http({
      method: "GET",
      url: "http://104.197.128.152:8000/v1/tracks"
    });
    return response;
  };
}
})();

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.