javascript - Angular Submit Syntax and message if no results found using MovieDB API -
i'm super new @ web development , trying learn how use angular mini project. i'm using moviedb's restful api's , want simple search movie title, return results found, , return message if nothing found.
unfortunately i'm hitting few comprehension hurdles , have looked solution haven't found 1 matches i'm doing, think pretty simple (most of ones i've seen more complex approaches , think it's beyond understanding moment)
all want right have searchbar, user types in movie name, clicks submit, , angular app return results. can't quite figure out how pass submit through app. here html code:
<!doctype html> <html> <head> <link href="http://s3.amazonaws.com/codecademy-content/projects/bootstrap.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"> </script> <!-- modules --> <script src="js/app.js"></script> <!-- controllers --> <script src="js/controllers/maincontroller.js"></script> <link href="css/main.css" rel="stylesheet" /> </head> <body ng-app="myapp"> <div class="header"> <img src="img\logo.png" style="width: 80px; height: 80px;> <div class="container"> <h1>the moviedb search tool</h1> <h2>search movie!</h2> </div> <div class="main" ng-controller='maincontroller'> <div class="searchbar"> <form ng-submit="submitfunction(field)"> <input type="text" name="field" placeholder="search movie"> <input type="submit" value="submit"> </form> </div> <div class="container"> <div ng-repeat="details in movielist" class="col-md-6"> <div class="thumbnail"> <img ng- src="https://image.tmdb.org/t/p/w154{{details.poster_path}}"> <p>{{ details.title }}</p> <p class="date">release date: {{ details.release_date | date }}</p> <p>{{details.overview}}</p> </div> </div> </div> </div> <div class="footer"> <div class="container"></div> </div> </body> </html>
and javascript code angular app:
app.controller('maincontroller', ['$scope', '$http', function($scope, $http) { $scope.movietitle = function submitfunction(field){ } $http.get('https://api.themoviedb.org/3/search/movie?api_key='+api_key+'&language=en-us&query=' +$scope.movietitle+'&page=1&include_adult=false') .then(successcallback, errorcallback); function successcallback(response){ $scope.movielist = response.data.results } function errorcallback(error){ } }]);
if plug in movie name 'deadpool' html works fine, of course static value runs automatically. , need work on submit, , way i'm trying isn't working. think need variable passed in $scope, buy submit function i'm not getting when run this.
if point, think figure out logic display message of no results found, should simple checking json object api returns , looking @ "total results" number, returning specific message (at least hope it's simple).
any , appreciated!
html:
<form ng-submit="submitfunction()"> <input type="text" ng-model="movietitle" placeholder="search movie"> <input type="submit" value="submit"> </form>
controller:
app.controller('maincontroller', ['$scope', '$http', function($scope, $http) { $scope.submitfunction = function(){ $http.get('https://api.themoviedb.org/3/search/movie?api_key='+api_key+'&language=en-us&query='+$scope.movietitle+'&page=1&include_adult=false') .then(successcallback, errorcallback); } function successcallback(response){ $scope.movielist = response.data.results } function errorcallback(error){ } }]);
Comments
Post a Comment