Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 使用.success在angular中加载json数据_Angularjs_Json_Promise - Fatal编程技术网

Angularjs 使用.success在angular中加载json数据

Angularjs 使用.success在angular中加载json数据,angularjs,json,promise,Angularjs,Json,Promise,我正在和pro angularjs的书一起学习angularjs,但很早就遇到了问题:我无法从json文件中获取数据。以下是HTML: <!DOCTYPE html> <html ng-app="demo"> <head> <title>Example</title> <script src="angular.js"></script> <link href="bootstrap.c

我正在和pro angularjs的书一起学习angularjs,但很早就遇到了问题:我无法从json文件中获取数据。以下是HTML:

<!DOCTYPE html>
<html ng-app="demo">
<head>
    <title>Example</title>
    <script src="angular.js"></script>
    <link href="bootstrap.css" rel="stylesheet" />
    <link href="bootstrap-theme.css" rel="stylesheet" />
    <script type="text/javascript">
        var myApp = angular.module("demo", []);
        myApp.controller("demoCtrl", function($scope, $http) {
            var promise = $http.get("todo.json");
            promise.success(function(data) {
                $scope.todos = data;
            });
        });
    </script>
</head>
<body ng-controller="demoCtrl">
    <div class="panel">
        <h1>To Do</h1>
        <table class="table">
            <tr><td>Action</td><td>Done</td></tr>
            <tr ng-repeat="item in todos">
                <td>{{item.action}}</td>
                <td>{{item.done}}</td>
            </tr>
        </table>
    </div>
</body>
</html>
该表显示时没有内容。 我读到
.success
已经过时,使用
。然后改为
,如下所示:

<script type="text/javascript">
    var myApp = angular.module("demo", []);
    myApp.controller("demoCtrl", function($scope, $http) {
        var promise = $http.get("todo.json");
        promise.then(function(data) {
            $scope.todos = data;
        }, function() {
            alert('No Todos');
        });
    });
</script> 

var myApp=angular.module(“demo”,[]);
控制器(“demoCtrl”,函数($scope,$http){
var promise=$http.get(“todo.json”);
promise.then(函数(数据){
$scope.todos=数据;
},函数(){
警报(“无待办事项”);
});
});

现在我的桌子上有6个空行。请帮助:)

。然后
将响应包装在对象中。使用
$scope.todos=data.data将其拉出工作完美!谢谢你的快速回复!
<script type="text/javascript">
    var myApp = angular.module("demo", []);
    myApp.controller("demoCtrl", function($scope, $http) {
        var promise = $http.get("todo.json");
        promise.then(function(data) {
            $scope.todos = data;
        }, function() {
            alert('No Todos');
        });
    });
</script>