【发布时间】:2018-11-15 15:36:48
【问题描述】:
我已经在原生 Javascript 中完成了这项工作,但我现在想在 Angular 中完成。
我想创建一个简单的应用程序,用户在其中输入城市名称,并获取该城市餐厅的输出以及餐厅名称和地址。我能够成功地输出带有预定义城市的数据,但我似乎无法通过用户输入来完成。我不确定如何将“city_name”的值放入控制器中的 url。
这是我目前所拥有的。我希望 div 内的表格内容仅在单击提交按钮时出现。
<!doctype html>
<html>
<head>
<title>Restaurant App</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
</head>
<body ng-app="restaurantApp">
<input type="text" ng-model="city_name"</input>
<button ng-click="submit()">Submit</button>
<div ng-controller="restaurantController">
<table>
<tr ng-repeat="restaurant in restaurantList">
<td>{{restaurant.name}}</td>
<td>{{restaurant.address}}</td>
<td>{{restaurant.price}}</td>
</tr>
</table>
</div>
<script>
//var city = 'chicago';
angular.module('restaurantApp', [])
.controller('restaurantController', function($scope, $http) {
$http.get(`http://opentable.herokuapp.com/api/restaurants?city=${$scope.city_name}`).
then(function(response) {
$scope.restaurantList = response.data.restaurants;
});
});
</script>
</body>
</html>
【问题讨论】: