1

i can get the input and fetch the json i can also insert the data to the $scope.movies

but i can't see the results. what am i missing?

thanks.

the html:

<!DOCTYPE html>
<head>
  <title>Learning AngularJS</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js" type="text/javascript"></script>
  <script src="app.js" type="text/javascript"></script>
  <script src="mainController.js" type="text/javascript"></script>
</head>
<body>
  <div id='content' ng-app='MyTutorialApp' ng-controller='MainController'>
    <input type='text' ng-model='searchMovies' />
    <button ng-click='addNew()'>Add</button>
    <ul>
        <li ng-repeat="movie in movies">1</li>
    </ul>
  </div>
</body>
</html>

the app:

var app = angular.module('MyTutorialApp',[])

the controller:

  app.controller("MainController", function($scope){
  $scope.movies = null;
  $scope.searchMovies = null;
  $scope.addNew = function() {
    if ($scope.searchMovies != null && $scope.searchMovies != "") {
        fetch('http://www.omdbapi.com/?s=' + $scope.searchMovies)
                .then(data => data.json())
                .then(data => {
                    console.log(data)
                    $scope.movies = data.Search
                console.log($scope.movies)});
    }
  }
});

edit: i'm using live-server to to run this code. maybe it's connected?

0

1 Answer 1

1

Try this:

...
$scope.movies = data.Search
$scope.$applyAsync()

Because you are using fetch and not $http, Angular does not notice that the scope object has been changed. $applyAsync manually schedules the change detection.

To avoid these hassles, I would recommend you to switch to $http.


Edit: $applyAsync is not available in Angular 1.0.7 - it works if you upgrade to a recent version like https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js.

Sign up to request clarification or add additional context in comments.

5 Comments

Instead of using $scope.$applyAsync(); he could wrap the promise in $q.when() for similar effect. Some might find that approach cleaner.
the $applyAsync() did not work. trying now the $http
Ah, you are using AngularJS 1.0.7 for some reason? That version is really outdated, can you upgrade?
@user4602966 Beware when using $http that the response is passed to the promise a bit differently than with fetch. You'll be using the promise like this instead: .then(response => response.data).
the different version and http fixed the problem :) thank you @stholzm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.