gpt4 book ai didi

javascript - 从 JSON 中获取数据并使用 Angular 进行过滤

转载 作者:行者123 更新时间:2023-11-29 19:20:22 25 4
gpt4 key购买 nike

我需要用 angular.js 开发一个简单的应用程序,但我遇到了很多麻烦,因为我不习惯使用 javascript..我的应用程序包含一个输入文本,我在其中写了一个系列/电影标题,然后我必须根据 JSON 说我得到了该 JSON 中有多少系列、电影和游戏。这是 JSON(您可以看到我正在谈论的 Type 属性):

{
"Search": [
{
"Title": "The Wire",
"Year": "2002–2008",
"imdbID": "tt0306414",
"Type": "series",
"Poster": "http://ia.media-imdb.com/images/M/MV5BNjc1NzYwODEyMV5BMl5BanBnXkFtZTcwNTcxMzU1MQ@@._V1_SX300.jpg"
},
{
"Title": "Wire in the Blood",
"Year": "2002–",
"imdbID": "tt0337792",
"Type": "series",
"Poster": "http://ia.media-imdb.com/images/M/MV5BMTg2NDc3ODI4MF5BMl5BanBnXkFtZTcwOTM3OTIzMQ@@._V1_SX300.jpg"
},
{
"Title": "Thru the Wire",
"Year": "1987",
"imdbID": "tt0094143",
"Type": "movie",
"Poster": "N/A"
},
{
"Title": "The Wire",
"Year": "1997–1998",
"imdbID": "tt0960700",
"Type": "series",
"Poster": "N/A"
},
{
"Title": "Over the Wire",
"Year": "1996",
"imdbID": "tt0114071",
"Type": "movie",
"Poster": "http://ia.media-imdb.com/images/M/MV5BMTY5MjE1NDQ0MF5BMl5BanBnXkFtZTcwODMyNjUxMQ@@._V1_SX300.jpg"
},
{
"Title": "The Wire: The Musical",
"Year": "2012",
"imdbID": "tt2191222",
"Type": "movie",
"Poster": "N/A"
},
{
"Title": "The Wire: The Chronicles",
"Year": "2007–",
"imdbID": "tt1168599",
"Type": "series",
"Poster": "N/A"
},
{
"Title": "Hold the Wire",
"Year": "1936",
"imdbID": "tt0027756",
"Type": "movie",
"Poster": "N/A"
},
{
"Title": "Tapping the Wire",
"Year": "2007",
"imdbID": "tt1075404",
"Type": "movie",
"Poster": "N/A"
},
{
"Title": "Wind in the Wire",
"Year": "1993",
"imdbID": "tt0324568",
"Type": "movie",
"Poster": "N/A"
}
]
}

例如,当我输入“The wire”时,我应该得到 4 个系列、0 个游戏和 6 个电影。我通过调用 IMDB API 来做到这一点,然后我访问类型元素并读取它是电影、游戏还是系列。到目前为止,我已经编写了这段代码,但它有一些奇怪的行为:

<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

<body>
<div ng-app="myApp" ng-controller="customersCtrl">TV Series:
<input type="text" ng-model="seriesName">
<br>
<br>Series Name: {{seriesName}}
<input type="submit" ng-click="search(seriesName)" value="Search" />
<ul>
<li ng-repeat="x in names | filter:isActive"></li>
</ul>
<br>Series:{{series}}
<br>
<br>Game:{{games}}
<br>
<br>Movie:{{movies}}
<br>
</div>

<script>

var app = angular.module('myApp', []);
app.controller('customersCtrl', function ($scope, $http) {
$scope.series = 0;
$scope.movies = 0;
$scope.games = 0;
$scope.search = function (value) {

$http.get("http://www.omdbapi.com/?s=" + value).success(function (response) {
$scope.names = response.Search;
});

$scope.isActive = function (x) {
if (x.Type === "movie") {
$scope.movies = $scope.movies + 1;
} else if (x.Type === "game") {
$scope.games = $scope.games + 1;
} else if (x.Type === "series") {
$scope.series = $scope.series + 1;
}
return false;
};
};
});

</script>

我遇到的第一个问题是,当我点击搜索时,我得到了 44 个系列而不是 4 个和 66 个电影而不是 6 个第二个问题是,在第一次点击后,当我再次输入时,我的系列、游戏和电影属性在我没有点击搜索按钮的情况下改变了它们的编号。

最佳答案

你得到 44 和 66 是因为你是在追加字符串而不是添加这些数字。在 Javascript 中,+ 运算符还连接字符串,因此 'foo'+1 = 'foo1'。和的两侧之一是字符串而不是整数。

此外,如果您只想在单击搜索按钮时搜索一次,我不会将 $scope.isActive 函数嵌套在 $scope.search 函数中。只需使搜索功能设置 $scope.names 变量,如果你还想设置电影游戏和系列计数器,在成功后立即通过迭代响应(我推荐 lodash 用于这样的事情)。

一旦范围内的搜索功能正确加载了所有数据,您就可以对其进行操作或呈现。关于 isActive 函数,我不完全确定你想在那里实现什么。但我认为您不需要它。

编辑:

app.controller('customersCtrl', function ($scope, $http) {
$scope.series = 0;
$scope.movies = 0;
$scope.games = 0;
$scope.search = function (value) {
$http.get("http://www.omdbapi.com/?s=" + value).success(function (response) {
$scope.names = response.Search;
$scope.movies = _.filter($scope.names,function(entry){
return entry.Type === 'movie'
}).length;
$scope.games = _.filter($scope.names,function(entry){
return entry.Type === 'game'
}).length;
$scope.series = _.filter($scope.names,function(entry){
return entry.Type === 'series'
}).length;
});
}

There you have the jsFiddle我该怎么做。

关于javascript - 从 JSON 中获取数据并使用 Angular 进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33350633/

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