繁体   English   中英

为什么我无法从Json文件中检索数据?

[英]Why can't I retrieve data from my Json file?

我正在尝试从我的json文件中获取一个小型测试项目的数据。 我在开发人员工具中没有收到任何错误,当我在请求后执行console.log(data)时,我从文件中获取数据。

这是我的角度代码:

var app = angular.module('myApp', []);

app.controller('MainController', ['$scope','$http', function($scope,     $http){
  $scope.todos = [];
  $http.get('db/todos.json')
    .then(function(data){
      $scope.todos = data;

    });
 }]);

这是我的html文件:

<!doctype html>
<html ng-app="myApp">
<head>
<base href="/">
<title>My todo</title>
<link rel="stylesheet"  href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://code.angularjs.org/1.5.5/angular.js"></script>
  <script src="/js/app.js"></script>
</head>
<body>
  <div class="container" ng-controller="MainController">
     <div class="row">
     <h1>Your todos</h1>
     <li ng-repeat="todo in todos">
        {{ todo.name }}
      <li>
    </div>
  </div>
</body>
</html>

在我的网站中,ng-repeat只打印出六个子弹点,这是错误的,因为我的json文件中只有两个条目。 这也是我从console.log(数据)获得的:

Object {data: Object, status: 200, config: Object, statusText: "OK"}
config: Object
data:Object
headers:(name)
status:200
statusText:"OK"
__proto__:Object

我可能错过了什么想法?

当使用.then ,实际的响应数据在response.data ,所以如果你改变它,你的代码就可以了:

.then(function(response){
    $scope.todos = response.data;
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM