gpt4 book ai didi

javascript - Angularjs - json内容不显示

转载 作者:行者123 更新时间:2023-12-02 16:08:32 25 4
gpt4 key购买 nike

这是我第一次使用 Angularjs 使用下面的代码显示 json 内容,

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>
<body>
<div ng-app="" ng-controller="GetVideosFiles">

<ul ng-repeat=" x in GetData ">
<li>example</li>
<li><h1>{{ x.videos.Title }}</h1></li>
<li>{{x.videos.Description}}</li><br><br>
<li><button>{{x.videos.Url}}</button></li>
</ul>


</div>

<script type="text/javascript">


function GetVideosFiles($scope,$http){
$http.get("VideoFile.json")
.success(function(rsp){
$scope.GetData = rsp;
});
}


</script>


</body>
</html>

VideoFile.json:

{ 
"videos": [{
"Title": "Windmill",
"Description": "What are wind mills? Are they giant fans? How do they work?",
"Url": "https://www.youtube.com/watch?v=Zrp0RC3XTpw"
}, {
"Title": "Race Car",
"Description": "Yeah, we know that your kid loves his cars",
"Url": "https://www.youtube.com/watch?v=zAteCGxrCSo"
}, {
"Title": "Blow Painting",
"Description": "The gentle wind has many an artist",
"Url": "https://www.youtube.com/watch?v=LKs3nw7YcR8"
}, , {
"Title": "Dreamcatcher",
"Description": "The wind turned naughty and blown the pieces of Dream catcher all over
the hose",
"Url": "https://www.youtube.com/watch?v=UbgZ­uDAmAM"
}]
}

但是这段代码对我不起作用。

我想像这样显示我的页面:

enter image description here

有人可以告诉我我做错了什么吗?

更新:

<div ng-app="app" ng-controller="GetVideosFiles">

<ul ng-repeat=" x in GetData.videos ">
<li>example</li>
<li><h1>{{ x.Title }}</h1></li>
<li>{{x.Description}}</li><br><br>
<li><a ng-href="{{ x.Url }}">Watch video</a></li>
</ul>


</div>

<script type="text/javascript">


var app=angular.module("app",[]);
app.controller("GetVideosFiles",GetVideosFiles);
function GetVideosFiles($scope,$http){
$http.get("VideoFile.json")
.success(function(rsp){
//alert(rsp);
$scope.GetData = rsp;
});
}

</script>

我已根据其中一个答案更新了我的代码,但它仍然无法正常工作。

最佳答案

你的 Controller 声明不正确,你的ng-repeat对象提及是错误的,并将你的json文件放在html的同一目录中。

尝试这样

<div ng-app="app" ng-controller="GetVideosFiles">

<ul ng-repeat=" x in GetData.videos ">
<li>example</li>
<li><h1>{{ x.Title }}</h1></li>
<li>{{x.Description}}</li><br><br>
<li><button>{{x.Url}}</button></li>
</ul>


</div>

<script type="text/javascript">

var app=angular.module("app",[]);
app.controller("GetVideosFiles",GetVideosFiles);
function GetVideosFiles($scope,$http){
$scope.GetData=[];
$http.get("VideoFile.json")
.success(function(rsp){
$scope.GetData = rsp;
});
}


</script>

关于javascript - Angularjs - json内容不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30474046/

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