如何使用AngularJS动态显示AJAX调用中的所有数据?

时间:2018-12-10 07:57:47

标签: angularjs ajax asp.net-web-api

我正在尝试通过餐厅名称搜索我的Web 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);

2 个答案:

答案 0 :(得分:1)

因此,您想循环js文件中的所有数据并希望附加数据。

   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>"));
   });

添加并确认您的html文件具有带有“ searchResults”作为ID的标签。 希望这能解决您的问题。

答案 1 :(得分:0)

为响应定义范围变量,然后在html中添加ng-repeat。

   Eg: ` 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;
                $scope.searchResult = response.data;

            },
                function (response) {   // error callback function
                    alert("ERROR: " + response.data);
                });`

然后在HTML PAGE中添加“ ng-repeat”用于循环结果。然后,您将基于搜索关键字获得所有结果。