繁体   English   中英

AngularJS中的$ scope在ng-repeat中不返回任何内容

[英]$scope in AngularJS returns nothing in ng-repeat

我试图返回一个对象,我在一个forEach循环中填充了一个角度$ scope,来自控制器,但是当我尝试使用ng-repeat将其循环出时,我没有任何结果。

当我console.log对象时,我得到了预期的结果

在此处输入图片说明

但是,当我尝试用$ scope返回它并用ng-repeat显示它时,却没有任何结果。

这是我的控制器

myAppControllers.controller('musicCtrl', ['$scope', '$http', function($scope, $http) {

var i = 0,
    playlists = {};

// Get the playlists from soundcloud
$http({ method: 'GET', url: 'http://api.soundcloud.com/users/gimle-sound-tjek/playlists.json?client_id=c2dfe07de1d18d689516884ce22b7aae' }).
    success(function(data) {
        data.forEach(function() {

            // Populate the object
            playlists[i] = {
                "title" : data[i].title,
                "permalink": data[i].permalink,
                "genre": data[i].genre
            }

            i++;
        });

        console.log(playlists);
        $scope.playlists;
    }).
    error(function() {
        $scope.playlists = '';
    });
}]);

我的ng-repeat看起来像这样

<div ng-repeat="playlist in playlists">
<h3>{{ playlist.title }}</h3>
...

我期望这与$ scope.playlists将对象发送回的方式有关吗?

首先,也许可以使用push代替这i++乐趣。

您可以将这些播放列表条目推入合并范围之外的playlist变量。

myAppControllers.controller('musicCtrl', ['$scope', '$http', function($scope, $http) {
    $scope.playlists= [];

// Get the playlists from soundcloud
$http({ method: 'GET', url: 'http://api.soundcloud.com/users/gimle-sound-tjek/playlists.json?client_id=c2dfe07de1d18d689516884ce22b7aae' }).
    success(function(data) {
        data.forEach(function(entry) {    
            // Populate the object
            $scope.playlists.push({
                "title" : entry.title,
                "permalink": entry.permalink,
                "genre": entry.genre
            });
            //OR: $scope.playlists.push(entry);
        });
    }).
    error(function() {
        $scope.playlists = '';
    });
}]);

如果您尝试$scope.playlists = []而不是var playlist = {}怎么办?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM