Я пытаюсь выполнить поиск в своем веб-API по названию ресторана, а затем отобразить все результаты, соответствующие этому названию.
Я должен использовать AngularJS.Я могу заставить его отображаться вручную, но как мне заставить его отображать динамически для всех данных, а не только для одного?
Я знаю, что должен использовать $ .each () для получения каждого из результатов данных, ноэто просто не работает.
var app = angular.module('restaurant', []);
function RestaurantsController($scope, $http) {
// Create a function assigned to btnSearch's click event (ng-click).
$scope.search = function (searchName) {
var rName = $scope.searchName;
var restName = $("#txtRestaurant").val();
var restaurant = new Object();
var strURL = "http://localhost:56475/api/Restaurants/GetByName/" + restName;
//scope this
var valid = true;
var field = $scope.searchName;
if (field == undefined) {
alert("Please enter a valid name.");
valid = false;
} else {
//.restaurantName has to match the parameter in my web service method
restaurant.name = rName;
restaurant = JSON.stringify(restaurant);
// Configure the request by creating a JavaScript object with the necessary properties
// and values for the request.
var request = {
method: "Get",
url: strURL,
headers: { // object containing header type as properties.
'Content-Type': "application/json; charset=utf-8",
},
data: restaurant // input parameter sent as JSON object.
};
// Setup and send an AJAX request that sends a search term
// used by the Web API to find a team.
$http(request).
then(function (response) { // success callback function
console.log(response);
$scope.restaurants = response.data;
var restaurants = response.data;
$.each(restaurants, function (index, restaurants) {
$("searchResults").append("<p>".concat("Restaurant ID: ", restaRestaurantID,
"<br>Restaurant Name: ", restaurants.RestName, "<br>Location: ", restaurants.RestAddr,
"<br>Star Rating: ", restaurants.StarRating, "<br>Price Rating: ", restaurants.PriceRating,
"<br>Restaurant : <br> <img src=", restaurants.ImageURL, " /><br>Cuisine: ", restaurants.Cuisine,
"<br>Average Rating: ", restaurants.AvgRating, "</p>"));
});
},
function (response) { // error callback function
alert("ERROR: " + response.data);
});
}
};
}
app.controller('RestaurantsController', RestaurantsController);