I need to develop a simple application with angular.js and I'm having a lot of trouble since I'm not used to use javascript.. My application consists of an input text where i write a series/movie title and then i have to say based on the JSON I get how many series, movies and games there are in that JSON. Here's the JSON(you can see the Type attribute I'm talking about):
{
"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"
}
]
}
For example when I type "The wire" I should get 4 series, 0 games and 6 movies. I do that by calling IMDB API and then I access the type element and read whether it is a movie, game or series. I've written this code so far but it has some weird behaviour:
<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>
First problem I have is that when i click search i get 44 series instead of 4 and 66 movies instead of 6 Second problem is that after clicking for the first time, as soon as i type again, my series,games and movies attributes change their number without me clicking the search button.