$ scope数组未在Angular中更新

问题描述

我有一系列定义为$ scope变量$ scope.movi​​esList的电影。我将数组中每个对象的所有imageSrc字段初始化为”(空字符串),并希望使用以下函数(API密钥已被清空)为每个对象更新此字段:

//loadMoviePosters calls the movie API for each movie in the moviesList array
//and sets the imageSrc field to the Poster link returned
$scope.loadMoviePosters = function(){
    for(var i = 0; i < $scope.moviesList.length; i++) {
        var movie = $scope.moviesList[i];
        $http({
            method: 'GET',url: "https://www.omdbapi.com/?t=" + movie.title + "&apikey=XXXXXXX"
        }).then(function success(response) {
            if (response.data.Poster != "N/A") {
                movie.imageSrc = response.data.Poster;
            }
            else {
                movie.imageSrc = '';
            }
            $scope.moviesList[i] = movie;
            //alert($scope.moviesList[i].imageSrc);
        },function error(respnonse) {
            //do nothing
        })
    }
    alert($scope.moviesList[0].imageSrc);
}

当我取消注释for循环内的警报时,它将显示数组中每个对象的图像链接,但是for循环外的警报始终显示一个空字符串。似乎$ scope.movi​​esList数组实际上并未在此函数中更新。

在我的HTML中,我有以下代码,在此函数更新$ scope数组之后,我想为每部电影显示海报:

<div ng-repeat = "movie in moviesList">
                <img ng-src = "{{movie.imageSrc}}" />
                <p>{{movie.title}}</p>
</div>

有人知道我如何获得loadMoviePosters函数来实际更新$ scope.movi​​esList数组吗?然后如何使这些图像显示在ng-repeat中?谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)