I'm trying to use AngularJS to get data from a json file which has video objects. However, I can only figure out based on other resources/references how to take ONE specific url and concatenate it using $sce to the http://youtube.com/embed/
link. I want to be able to store links to multiple videos in a json file and be able to get those links and insert them into the text provided below. I want to be able to insert something within the $scope.video = ... of the bottom set of code, but I can't figure out what to place there.
I know I need to make an $http request, something similar to this in order to get the data:
myApp.controller('mainController', ["$scope", "$http", function($scope, $http) {
$http.get('json/data.json').then(function(resource) {
$scope.videoList = resource.items;
});
json/data.json looks like this:
{
"items": [{
"id": 1,
"name": "Fall Afresh",
"url": "8VdXLM8H-xU",
"width": 420,
"height": 315,
"type": "video",
"provider_name": "http://youtube.com/"
}, {
"id": 2,
"name": "Furious",
"url": "bhBs1_iCF5A",
"width": 420,
"height": 315,
"type": "video",
"provider_name": "http://youtube.com/"
}, {
"id": 3,
"name": "God of the Redeemed",
"url": "m1n_8MUHZ0Q",
"width": 420,
"height": 315,
"type": "video",
"provider_name": "http://youtube.com/"
}, {
"id": 4,
"name": "Be Enthroned",
"url": "98cNpM-yh-I",
"width": 420,
"height": 315,
"type": "video",
"provider_name": "http://youtube.com/"
}, {
"id": 5,
"name": "This is Amazing Grace",
"url": "Bn5zk3yCRr0",
"width": 420,
"height": 315,
"type": "video",
"provider_name": "http://youtube.com/"
}]
}
var myApp = angular.module('myApp', []);
myApp.controller('mainController', function($scope) {
$scope.video = 'H_TnSwrNeWY';
});
myApp.directive('angularYoutube', function($sce) {
return {
restrict: 'E',
scope: {
video: '='
},
replace: true,
template: '<div style="height:300px;"><iframe style="overflow:hidden;height:100%;width:100%" width="100%" height="100%" src="{{url}}" frameborder="1" allowfullscreen></iframe></div>',
link: function(scope) {
console.log('here');
scope.$watch('video', function(newVal) {
if (newVal) {
scope.url = $sce.trustAsResourceUrl("http://www.youtube.com/embed/" + newVal);
}
});
}
}
});
<!DOCTYPE html>
<html lang="en-us" ng-app="myApp">
<head>
<title>Display JSON Data using AngularJS</title>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta charset="UTF-8">
<!-- load bootstrap and fontawesome via CDN -->
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<!-- load angular via CDN -->
<script src="https://code.angularjs.org/1.6.0/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<header>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/">JSON Data with AngularJS</a>
</div>
</div>
</nav>
</header>
<div class="container">
<div ng-controller="mainController">
<angular-youtube video="video">
</angular-youtube>
</div>
</div>
</body>
</html>
Several issues in code shown.
Use
ng-src
on the iframe to prevent bad requests when url is not availableTo access the data inside the $http
resource
object you need to first access thedata
propertyAs a start point you could then set the first video by doing:
Working demo