2017-03-15 40 views
1

我試圖撥打hotel_name使用ng-repeat。我該怎麼做。如何在此JSON數據上使用ng-repeat

這是代碼。但沒有任何顯示。

<div ng-repeat="x in hotels"> 
    <p ng-repeat="y in x.data">{{y.hotel_name}}</p> 
</div> 

這是數據格式。

{ 
status: 200, 
message: "Packages Found", 
data: [ 
    { 
    hotel_id: "40", 
    company_id: "2", 
    user_id: "17", 
    hotel_name: "Zen International", 
    hotel_description: "Good Hotel" 
    } 
] 
} 

回答

2

您的hotels似乎是一個對象。你不需要迭代它。

轉換這個

<div ng-repeat="x in hotels"> 
    <p ng-repeat="y in x.data">{{y.hotel_name}}</p> 
</div> 

這個

<div> 
    <p ng-repeat="y in hotels.data">{{y.hotel_name}}</p> 
</div> 
+0

由於dataListwith視圖這個工作 – DragonBorn

2

您可以我們一個NG-重複

<div> 
    <p ng-repeat="y in hotels.data">{{y.hotel_name}}</p> 
</div> 

DEMO

var demoApp = angular.module('demoApp', []); 
 
var controllers = {}; 
 
controllers.SimpleController = function ($scope) { 
 
    $scope.hotels = { 
 
status: 200, 
 
message: "Packages Found", 
 
data: [ 
 
    { 
 
    hotel_id: "40", 
 
    company_id: "2", 
 
    user_id: "17", 
 
    hotel_name: "Zen International", 
 
    hotel_description: "Good Hotel" 
 
    } 
 
] 
 
}; 
 
}; 
 
demoApp.controller(controllers);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script> 
 
<div ng-app="demoApp" ng-controller="SimpleController"> 
 
<div> 
 
    <p ng-repeat="y in hotels.data">{{y.hotel_name}}</p> 
 
</div> 
 
</div>

+0

由於這個工作 – DragonBorn

0

綁定Datalist中與控制器$範圍等

angular.module('YourApp', []) 
.controller('YourCtrl', function($scope) { 

    $scope.dataList={ 
    status: 200, 
    message: "Packages Found", 
    data: [ 
     { 
     hotel_id: "40", 
     company_id: "2", 
     user_id: "17", 
     hotel_name: "Zen International", 
     hotel_description: "Good Hotel" 
     } 
    ] 
    }  

}); 

和結合等

<div ng-app="YourApp" ng-controller="YourCtrl"> 
    <div> 
     <p ng-repeat="item in dataList.data">{{item.hotel_name}}</p> 
    </div> 
</div>