在AngularJS中显示对象属性

时间:2017-05-10 17:46:47

标签: javascript angularjs

我正在尝试将AngularJS中的信息显示为

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

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

每个对象都包含

之类的值
0:Object
genres: Array(1)
id: 84
title: "name"

未显示输出。它没有显示任何东西。 Fiddle

3 个答案:

答案 0 :(得分:1)

尝试 console.log(JSON.stringify(list.tracks));

答案 1 :(得分:0)

我做了一个使用angular

记录对象或数组的指令

设置$rootScope.debug = true以显示它! (或删除视图中的ng-if="$root.debug"

用法:

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

在您的控制器中

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

指令代码

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"
   }
});

html代码

<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>

答案 2 :(得分:0)

我使用$scope并以此方式工作。

<强> 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>

角度文件

(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;
  };
}
})();