【问题标题】:How to get data to html file by $http.get() from .json file如何通过 $http.get() 从 .json 文件获取数据到 html 文件
【发布时间】:2018-10-18 03:33:29
【问题描述】:

我试图通过使用$http.get() 方法从data/states.json 文件中提取数据来显示<li>ng-repeat 列表。谁能帮我解决这个问题。

HTML

<div ng-app="app" ng-controller="ctrl">
        <ul>
            <li ng-repeat="x in state">{{ x }}</li>
        </ul>
    </div>
    <script>
       var app = angular.module("app",[]);
       app.controller("ctrl", function($scope, $http) {
           $http.get("data/states.json").then(function(response) {
               $scope.state = response.data;
           });
       });
    </script>

.json 文件

[ "Alabama (AL)",
"Alaska (AK)",
"Arizona (AZ)",
"Arkansas (AR)" ]

【问题讨论】:

    标签: html angularjs json angular http


    【解决方案1】:

    您可以直接从响应中获取数据,如下所示,

    var app = angular.module('myApp', []);
    app.controller('ctrl', function($scope, $http) {
      $http.get("data.json")
      .success(function (response) {$scope.state = response;})
      .error(function (response) {alert("Error")})
    });
    

    PLUNKER DEMO

    【讨论】:

    • 查看上面的演示
    • 查找 A按州停车

    • var app = angular.module("myApp", []); app.controller("myCtrl", function($scope, $response){ $http.get("data/states.json") .success(function(response) { $scope.state = response ; }) });
    猜你喜欢
    • 2014-06-03
    • 2016-05-25
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    相关资源
    最近更新 更多