Javascript 如何放置来自json的注释?
我希望屏幕显示预订列表。完整列表位于.json文件中。预订通过输入缩写的机场名称进行选择。问题是我们显示了条目的名称,但没有显示机场的全名 my reservations.htmlJavascript 如何放置来自json的注释?,javascript,angularjs,json,Javascript,Angularjs,Json,我希望屏幕显示预订列表。完整列表位于.json文件中。预订通过输入缩写的机场名称进行选择。问题是我们显示了条目的名称,但没有显示机场的全名 my reservations.html <h3>Reserve for flight</h3> <div class="controls controls-row"> <input type="text" class="input-small" placeholder="Origin" ng-model="r
<h3>Reserve for flight</h3>
<div class="controls controls-row">
<input type="text" class="input-small" placeholder="Origin" ng-model="reserve.origin">
<input type="text" class="input-small" placeholder="Destination" ng-model="reserve.destination">
</div>
<a href="" class="btn btn-primary" ng-click="reserveFlight()">Reserve</a>
<h3>Your Reservations</h3>
<table ng-show="reservations.length" class="table">
<thead>
<tr>
<th>Number</th>
<th>Origin</th>
<th>Destination</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="flight in reservations">
<td>{{flight.number}}</td>
<td>
<a ng-href="#/airports/{{flight.origin}}">
{{flight.origin + ' - ' + flight.originFullName}}
</a>
</td>
<td>
<a ng-href="#/airports/{{flight.destination}}">
{{flight.destination + ' - ' + flight.destinationFullName}}
</a>
</td>
</tr>
</tbody>
</table>
<div ng-hide="reservations.length" class="alert alert-info">
You don't currently have any reservations, why not make one now?
</div>
flights.json文件
{
"number": 112,
"origin": "ATL",
"destination": "LAX",
"price": 232,
"originFullName": "Hartsfield Jackson Atlanta International Airport",
"destinationFullName": "Los Angeles International Airport"
},
{
"number": 812,
"origin": "ATL",
"destination": "JFK",
"price": 192,
"originFullName": "Hartsfield Jackson Atlanta International Airport",
"destinationFullName": "John F. Kennedy International Airport"
}, ...
这是我们现在展示的
应该是这样的
当您获得JSON时,您将使用空数组覆盖数据,从而丢失所有数据:
$http.get('data/flights.json').then(function(response){
$scope.reservations = response.data;
$scope.reservations = [];
我不知道你为什么这么做,但很自然,这会导致你在数组中没有任何东西。它将是空的
因此,您将只看到通过单击按钮添加的预订。但是,在要添加的对象中没有“originFullName”和“destinationFullName”,只有短名称
我无法修复你的代码,因为基本逻辑是错误的。您可以为全名再添加两个文本框,也可以不清除数组,您将看到现有数据。(但在添加新项目时仍会看到部分内容。)我对标题感到非常困惑。这完全与这个问题无关。
$http.get('data/flights.json').then(function(response){
$scope.reservations = response.data;
$scope.reservations = [];