Jérémie Chazelle Jérémie Chazelle - 5 months ago 35
jQuery Question

Use library (Plyr) with directive and AngularJS

I would like to use Plyr with AngularJS, but when I install plyr with AngularJS, Plyr doesn't work

I must include :

<html>
<head>
<link rel="stylesheet" href="https://cdn.plyr.io/1.7.0/plyr.css">

</head>
<body>

<script src="https://cdn.plyr.io/1.7.0/plyr.js"></script>

<div id="plyr" data-type="youtube" data-video-id="bTqVqk7FSmY"></div>

<script>
var player = plyr.setup(document.querySelector('#plyr'), {
debug: true
});

</script>


</body>
</html>


I tried to create a directive :

(function (){
'use strict';

angular.module('ngPlyr', []);

angular.module('ngPlyr').directive('plyr', function () {
return {
template: '<div id="plyr" data-type="youtube" data-video-id="bTqVqk7FSmY"></div>'
};
})

})();


and

<div plyr></div>

Answer

You need to initialize Plyr inside your directive in it's link function. Something like this should work:

(function (){
    'use strict';

    angular.module('ngPlyr', []);

    angular.module('ngPlyr').directive('plyr', function () {
        return {
            template: '<div data-type="youtube" data-video-id="bTqVqk7FSmY"></div>',
            link: function(scope, element, attrs) {
                var player = plyr.setup(element, { debug: true });
            }
        };
    })

})();