I'm trying to create a app using Angular that allowes user to search for a movie title and the app should show all the results corrosponding to that searchterm.
I've created a controller that does a JSON request to the tmdb database and in my view I show all the titles from that request.
The problem is that I can't get the user input to do the search. In my controller I've created a variable called searchquery. If I set the value to var searchquery = "james bond" and then visit the app the the controller does a JSON request to the tmdb database and asks for all "james bond" titles and then shows all the titles on my frontpage.
So my question is how can I use the user input to change that variable so the user can search for other titles?
In my scope I have a variable called search which inserts a variable called searchquery. I want to edit the value of searchquery so it searches the input of the user.
The controller,
angular.module('app.exampleApp').controller('exampleCtrl', [
'$scope', '$http', function($scope, $http) {
var base = 'http://api.themoviedb.org/3';
var service = '/search/movie';
var apiKey = 'a8f7039633f2065942cd8a28d7cadad4&query='
var search = [searchquery]
var callback = 'JSON_CALLBACK'; // provided by angular.js
var url = base + service + '?api_key=' + apiKey + search + '&callback=' + callback;
$scope.movieList = ["Searching ..."];
$http.jsonp(url).
success(function (data, status, headers, config) {
if (status == 200) {
$scope.movieList = data.results;
console.log($scope.movieList)
} else {
console.error('Error happened while getting the movie list.')
}
}).
error(function (data, status, headers, config) {
console.error('Error happened while getting the movie list.')
});
}
]);
var searchquery = document.getElementById('searchquery');
console.log(searchquery)
And the show,
%h1
Logo
%li
= link_to('Logout', destroy_user_session_path, :method => :delete)
#search{"ng-app" => "app.exampleApp"}
%input{"ng-model" => "message", :type => "text", :id => "searchquery"}
%p {{ message }}
%div{"ng-controller" => "exampleCtrl"}
%div{"ng-repeat" => "movie in movieList"}
{{ movie.original_title }}
I have a input with an id called searchquery this input should be placed in the variable search in the controller so it changes the JSON request to the input of the user.
I thought maybe I can find the input of the element (the input) and store it in the variable. But the console log outputs nill.
Also this is just JS and not Angular, and I'm thinking there's an Angular way to solve this problem as well.
console.log( data );give?"ng-model" => "message". You can access this as $scope.message inside your controller.var search = $scope.searchquery = '';into my controller but when I type in my input field nothing happens. Alsoconsole.log('user input : ' + $scope.searchquery); //only showsuser input :So the scope.searchquery in the controller doesn't get the input from the field.