使用AngularJS加载JSON文件

win*_*ova 5 html javascript json angularjs

我刚刚开始学习Angular并且我已经在SO上寻找使用angular加载JSON文件的解决方案,我已经完成了其他人发布的解决方案,但我无法从我的json文件中获取数据以显示某些原因.

我的json文件(test.json)很简单:

{
    "array": [
        {"thing": "thing1"},
        {"thing": "thing2"},
        {"thing": "thing3"}
    ],

    "name": "robert"
}
Run Code Online (Sandbox Code Playgroud)

这是我的js文件:

var myMod = angular.module("myMod", []);

myMod.controller("myCont", function ($scope, $http) {
    $scope.thing = "hi";

    $http.get("/test.json")
            .success(function (data) {
                $scope.stuff = data.array;
                $scope.name = data.name;
            })
            .error(function (data) {
                console.log("there was an error");
            });
});
Run Code Online (Sandbox Code Playgroud)

我试图只显示这样的名称,但只{{name}}显示:

<html ng-app="myMod">
    <head>
        <script src="angular.js"></script>
        <script src="testing.js"></script>
    </head>

    <body ng-controller="myCont">
        {{stuff}}
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Pan*_*kar 5

我认为你有错别字,你应该注入$http(负责进行ajax调用)依赖而不是$html(不存在于angular中)

您应该以这种方式更改代码。

myMod.controller("myCont", function ($scope, $html) {
Run Code Online (Sandbox Code Playgroud)

myMod.controller("myCont", function ($scope, $http) {
Run Code Online (Sandbox Code Playgroud)