2016-11-25 62 views
0

我想通過角度讀取json的值並在UI上顯示數據。我無法這樣做,因爲我得到的UI格式不是數組。當我添加到控制檯並查看格式時,它有點不同。我試圖在控制檯上玩「c」,無法玩這個物體。如何在我的網頁上顯示{{詳情}}的任何提示?angularjs從服務中獲取正確的格式數據

enter image description here

<script> 
    var app = angular.module('myApp', []); 
    app.controller('myCtrl', function ($scope, $http, MyService) { 

      $scope.details = MyService.getDetails(); 
      console.log($scope.details); 

    }); 
    app.service('MyService', function ($http) { 
     this.getDetails = function (x, y) { 

      return $http.get("/Home/GetMyData") 
        .then(function (response) { 
         return response.data; 
        }); 
     } 
    }); 


</script> 

     public JsonResult GetMyData() 
     { 
      var details = GetDet(); 
      return Json(details, JsonRequestBehavior.AllowGet); 
     } 
+1

數組內名爲「value」的對象內部是什麼?屬性?我的意思是,這些對象內的屬性是什麼? – lealceldeiro

+0

是的,我需要數據內的價值 – Kurkula

+0

你可以創建一個填充代碼的小提琴嗎? – Yaser

回答

1

MyService.getDetails返回一個承諾,所以下面只代碼顯示了承諾對象

$scope.details = MyService.getDetails(); 
console.log($scope.details); // promise object 

你需要這樣做來獲得getDetails的解析值:

MyService 
    .getDetails() 
    .then(function(details){ 
     $scope.details = details; 
     console.log($scope.details); // your array 
    }); 

我希望這有助於!