The value of the function return from the service $ http GET

I am trying to return an array from a function after running onClick . I see the values ​​contained in the array in the console. But I do not see the values ​​displayed on the HTML page.

Controller:

$scope.chosenFilm = []; //The array where values are stored

  //onClick function
  $scope.insertFilm = function () {  
     console.log("Film saved in watch list!");
     getSelectedFilm(); //Call function
}

function getSelectedFilm() {
  return $http.get('/watch-list').then(function(response) {
      $scope.chosenFilm = response.data;     
      $log.info($scope.chosenFilm[0]); // Values displayed in console!
        return $scope.chosenFilm; 
      });
}

HTML page where the values ​​will be displayed:

<div class="container">
        <table class="table">
          <thead>
            <tr>
              <th>Film</th>
              <th>Poster</th>
              <th>Overview</th>
              <th>Genres</th>
              <th>Release</th>
            </tr>
          </thead>
          <tbody>
            <tr ng-repeat="film in chosenFilm">
              <td>{{film.title}}</td>
              <td><img ng-src="{{film.poster}}"></td>
              <td>{{film.overview}}</td>
              <td>{{film.genres}}</td>
              <td>{{film.release |  date:  "dd.MM.y" : UTC+00:00 }}</td>
            </tr>
          </tbody>
        </table>
</div>
+4
source share
4 answers

This method returns the value of the promise and is asynchronous. Because of this, you need to use htpp.get methods with callback functions.

As a result, regarding your code, you can use

function getSelectedFilm() {
  $http.get('/watch-list').then(function(response) {
      $scope.chosenFilm = response.data;     
      $log.info($scope.chosenFilm[0]); // Values displayed in console!
        $scope.chosenFilm; 
      })
 .then (() => { 
console.log($scope.chosenFile);
......something else...; // you can see the  data 
});
+1
source

getSelectedFilm()calling HTTP GET not executing

insertFilm(),

+1

. , angular $scope , ng-repeat, .. $scope.chosenFilm ng -repeat . : ng-repeat . , , Film ( ) response.data. , :

angular.extend($scope.chosenFilm, response.data);
+1

$scope.chosenFilm = []; //The array where values are stored

 //onClick function
 $scope.insertFilm = function () {  
 console.log("Film saved in watch list!");
 $http.get('/watch-list')
 .then(function(response) {
      $scope.chosenFilm = response.data;     
      $log.info($scope.chosenFilm[0]); // Values displayed in console!
  });
}
+1

Source: https://habr.com/ru/post/1682887/


All Articles