gpt4 book ai didi

javascript - 使用 AngularJS 加载 JSON 文件

转载 作者:太空狗 更新时间:2023-10-29 15:59:30 26 4
gpt4 key购买 nike

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

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

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

"name": "robert"
}

这是我的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");
});
});

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

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

<body ng-controller="myCont">
{{stuff}}
</body>
</html>

最佳答案

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

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

myMod.controller("myCont", function ($scope, $html) {

myMod.controller("myCont", function ($scope, $http) {

关于javascript - 使用 AngularJS 加载 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38020197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com