I'm trying to get this code to provide a value in my select ng-options:
<select ng-model="selectedMovieId" ng-if="movies.length>0" ng-options="movie.id as movie.title for movie in movies track by movie.id"></select>
<div>
{{selectedMovieId||'No movie selected'}}
</div>
But I never see a value for selectedMovieId. I've tried adding a dot to the ng model (something like foo.selectedMovieId) but I keep getting errors.
Having read 3-4 questions on this I feel like it has to be something simple I'm missing.
Here's my full code:
var app = angular.module("movieApp", []);
app.controller("movieCtrl", ["$scope", "$http", function($scope, $http) {
$scope.apiKey = ''
var baseUrl = 'https://api.themoviedb.org/3/'
$scope.movies = []
$scope.searchMovie = function() {
var url = baseUrl + 'search/movie?api_key=' + $scope.apiKey + '&query=' + $scope.queryString;
$http.get(url)
.then(function(response) {
$scope.movies = response.data.results;
}, function() {
console.log("some error");
})
}
$scope.getCredits = function() {
var url = baseUrl + 'movie/' + $scope.selectedMovieId + '/credits?api_key=' + $scope.apiKey
console.log(url)
console.log($scope.movies)
/*$http.get(url)
.then(function(response) {
console.log(response.data)
$scope.actors = response.data.results;
}, function() {
console.log("some error");
})*/
}
}]);
and html:
<div ng-app="movieApp" ng-controller="movieCtrl">
<input type='text' ng-model='queryString' ng-submit="searchMovie()">
<button ng-click="searchMovie()">
Search
</button>
<hr/>
<select ng-model="selectedMovieId" ng-if="movies.length>0" ng-options="movie.id as movie.title for movie in movies track by movie.id">
</select>
<div>
{{selectedMovieId||'No movie selected'}}
</div>
<button ng-if="movies.length>0" ng-click="getCredits()">
Get Credits
</button>
</div>
And the jsfiddle I've been working in. Why won't the ng-model update?