【发布时间】:2018-01-10 03:38:48
【问题描述】:
一旦 onClick 被触发,我试图从函数中返回一个数组。我在控制台中看到数组中包含的值。但我没有看到 HTML 页面上显示的值。
控制器:
$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 页面:
<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>
【问题讨论】:
-
你是在告诉 html 使用哪个控制器吗?
-
尝试在
http服务之前删除return。而且你也不需要返回数组。刚好够$scope.chosenFilm = response.data; -
@Hadi,但在这种情况下这不是问题。它不应该干扰元素的渲染
-
嗨 @DanielD 是的,我是。
-
如果能给我们提供Minimal, Complete, and Verifiable example就很好了
标签: javascript angularjs http-get angularjs-http