Dev*_*ght 0 javascript arrays angularjs angularjs-ng-repeat
我调用一个返回单个对象的API端点.我想循环一个名为video存储在对象中的数组,并将存储在数组中的所有链接返回给视图.
从API返回的JSON对象
HTML代码
<div class="myVideo" ng-repeat="v in courses.video">
<iframe width="560" height="315" ng-src="{{'v.video'}}"
frameborder="10" allowfullscreen></iframe>
</div>
Run Code Online (Sandbox Code Playgroud)
用于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);
});
};
Run Code Online (Sandbox Code Playgroud)
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);
});
};
Run Code Online (Sandbox Code Playgroud)