如何使用ng-repeat

时间:2017-03-13 14:48:41

标签: javascript angularjs arrays angularjs-ng-repeat

我调用一个返回单个对象的API端点。我想循环存储在对象中的名为video的数组,并将存储在数组中的所有链接返回给视图。

从API返回的JSON对象

enter image description here

html代码

<div class="myVideo" ng-repeat="v in courses.video">
     <iframe width="560" height="315" ng-src="{{'v.video'}}" 
      frameborder="10" allowfullscreen></iframe>
</div>

API调用控制器中的函数

$scope.getCourse = function(id){
        coursesFac.getCourseById(id)
            .then(function (response) {
                $scope.courses = response.data;
                var items =response.data;
                console.log(items);
                //console.log($scope.courses.video);
            }, function (error) {
                $scope.status = 'Unable to load course data: ' + error.message;
                console.log($scope.status);
            });
    };

视频应显示的视图上会显示此错误

1 个答案:

答案 0 :(得分:6)

courses.video是一个字符串 - 不是数组。你需要解析json

$scope.getCourse = function(id) {
    coursesFac.getCourseById(id)
        .then(function(response) {
            response.data.video = JSON.parse(response.data.video); //HERE
            $scope.courses = response.data;
            var items = response.data;
            console.log(items);
            //console.log($scope.courses.video);
        }, function(error) {
            $scope.status = 'Unable to load course data: ' + error.message;
            console.log($scope.status);
        });
};
相关问题