Javascript TypeError:在$http.get JSON文件时无法读取未定义的属性“value”

Javascript TypeError:在$http.get JSON文件时无法读取未定义的属性“value”,javascript,json,angularjs,html,ionic,Javascript,Json,Angularjs,Html,Ionic,简报: 当从angularJS读取JSON文件时,它会加载,但在数据可用或类似奇怪的情况出现之前,它会花费一些时间 JSON是: { "number": 1, "persons":[ { "gender": "Male", "name": "Paul", } ] } 角度代码为: app.controller('myapp', function($scope,$http){ var folder = 'DB/'; $http.

简报: 当从angularJS读取JSON文件时,它会加载,但在数据可用或类似奇怪的情况出现之前,它会花费一些时间

JSON是:

{
  "number": 1,
  "persons":[
    {
      "gender": "Male",
      "name": "Paul",
    }
  ]
}
角度代码为:

app.controller('myapp', function($scope,$http){
    var folder = 'DB/';
    $http.get(folder+'database.json').success(function(data){
        $scope.DataBase= data;
    });

    console.log($scope);

    $scope.getname= function(){
        return $scope.DataBase.persons[0].name;
    }
html是:

<body> 
    <div class="d_name" ng-controller="myapp">
        {{getname()}}
    </div>
</body>
我不明白的是,为什么在获取JSON之后,console.log$scope显示存在对象数据库 但是函数getname在工作之前会出现两个错误,就像有一段时间范围没有更新,getname函数没有找到对象一样,但是为什么日志会显示它呢

控制台:

ChildScope {$$childTail: null, $$childHead: null, $$nextSibling: null, $$watchers: null, $$listeners: Object…}
$$ChildScope: null
$$childHead: null
$$childTail: null
$$listenerCount: Object
$$listeners: Object
$$nextSibling: ChildScope
$$prevSibling: null
$$watchers: Array[3]
$$watchersCount: 3
$id: 4
$parent: ChildScope
DataBase: Object
persons: Array[1]
number: 1
__proto__: Object
getgen: ()
getimg: ()
getname: ()
__proto__: ChildScope

ionic.bundle.js:21157 
TypeError: Cannot read property 'persons' of undefined
    at Scope.$scope.getname (http://localhost:63342/test/www/js/controllers/c_menu.js:27:35)
    at fn (eval at <anonymous> (http://localhost:63342/test/www/lib/ionic/js/ionic.bundle.js:21972:15), <anonymous>:4:209)
    at Object.expressionInputWatch [as get] (http://localhost:63342/test/www/lib/ionic/js/ionic.bundle.js:22954:31)
    at Scope.$digest (http://localhost:63342/test/www/lib/ionic/js/ionic.bundle.js:24502:40)
    at Scope.$apply (http://localhost:63342/test/www/lib/ionic/js/ionic.bundle.js:24778:24)
    at done (http://localhost:63342/test/www/lib/ionic/js/ionic.bundle.js:19191:47)
    at completeRequest (http://localhost:63342/test/www/lib/ionic/js/ionic.bundle.js:19363:7)
    at XMLHttpRequest.requestLoaded (http://localhost:63342/test/www/lib/ionic/js/ionic.bundle.js:19304:9)

ionic.bundle.js:21157 
TypeError: Cannot read property 'persons' of undefined
    at Scope.$scope.getname (http://localhost:63342/test/www/js/controllers/c_menu.js:27:35)
    at fn (eval at <anonymous> (http://localhost:63342/test/www/lib/ionic/js/ionic.bundle.js:21972:15), <anonymous>:4:209)
    at Object.expressionInputWatch [as get] (http://localhost:63342/test/www/lib/ionic/js/ionic.bundle.js:22954:31)
    at Scope.$digest (http://localhost:63342/test/www/lib/ionic/js/ionic.bundle.js:24502:40)
    at Scope.$apply (http://localhost:63342/test/www/lib/ionic/js/ionic.bundle.js:24778:24)
    at done (http://localhost:63342/test/www/lib/ionic/js/ionic.bundle.js:19191:47)
    at completeRequest (http://localhost:63342/test/www/lib/ionic/js/ionic.bundle.js:19363:7)
    at XMLHttpRequest.requestLoaded (http://localhost:63342/test/www/lib/ionic/js/ionic.bundle.js:19304:9)

您的问题是因为$http是异步工作的。试试这个:

控制员:

html是:

<body> 
    <div class="d_name" ng-controller="myapp">
        {{getname()}}
    </div>
</body>

嗨,埃米尔,你在哪里很快。。。但是为什么控制台在正确显示对象之后?这段代码是一个简化的代码,每个人有5个字段,其中有50多个字段,JSON根中还有其他字段。您的问题不在$scope中,而在$scope.DataBase中。这是indefinedbut$scope.DataBase与日志中的所有字段一起出现尝试此测试:console.log$scope.DataBase在查看控制台后返回空对象,因此Quentin正确地将其标记为重复,这是一个异步问题
<body> 
    <div class="d_name" ng-controller="myapp">
        {{namePerson}}
    </div>
</body>