javascript - Audio tag, how to handle it from Angular - Stack Overflow

I am using the HTML5 audio tag, but I want to control it from Angular, according to the guidelines I do

I am using the HTML5 audio tag, but I want to control it from Angular, according to the guidelines I don't have to touch the DOM from the Controllers so I have to create a Directive.

Let's say I have this 2 buttons

<button onclick="playMusic()" type="button">Play Music</button>
<button onclick="pauseMusic()" type="button">Pause Music</button>

and this in the JS part

var music = document.getElementById("myMusic"); 

function playMusic() { 
    music.play(); 
} 

function pauseMusic() { 
    music.pause(); 
} 

but I need to transcript that into Angular, so, how can I create a directive for that ? or hoy can I implement it in a Controller ?

I am using the HTML5 audio tag, but I want to control it from Angular, according to the guidelines I don't have to touch the DOM from the Controllers so I have to create a Directive.

Let's say I have this 2 buttons

<button onclick="playMusic()" type="button">Play Music</button>
<button onclick="pauseMusic()" type="button">Pause Music</button>

and this in the JS part

var music = document.getElementById("myMusic"); 

function playMusic() { 
    music.play(); 
} 

function pauseMusic() { 
    music.pause(); 
} 

but I need to transcript that into Angular, so, how can I create a directive for that ? or hoy can I implement it in a Controller ?

Share Improve this question asked Apr 25, 2015 at 23:25 NonNon 8,61920 gold badges80 silver badges130 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

In angular, any DOM manipulation should really be handled in a directive (see documentation). This is to separate concerns and to improve the testability of other angular actors such as controllers and services.

A basic directive to play audio might look something like this (fiddle):

app.directive('myAudio', function() {
    return {
        restrict: 'E',
        link: function(scope, element, attr) {
            var player = element.children('.player')[0];
            element.children('.play').on('click', function() {
                player.play();
            });
            element.children('.pause').on('click', function() {
                player.pause();
            });
        }
    };
});

And the associated HTML:

<my-audio>
    <audio>
        <source src="demo-audio.ogg" />
        <source src="demo-audio.mp3" />
    </audio>
    <button class="play">Play Music</button>
    <button class="pause">Pause Music</button>   
</my-audio>

You can implement it in the controller, but you'd need to perform your DOM manipulations in there...which is exactly what you're trying to avoid.

https://docs.angularjs/guide/directive

http://ng-learn/2014/01/Dom-Manipulations/

Some relevant code, which might be useful to look out for while you read the last one:

    element = angular.element('<div class="form" data-my-slide="showForm">Text</div>');

and here ...

    link: function(scope, element, attrs) {

      // We dont want to abuse on watch but here it is critical to determine if the parameter has changed.
        scope.$watch(attrs.mySlide, function(newValue, oldValue) {

      // This is our logic. If parameter is true slideDown otherwise slideUp.

Hope that helps!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745324701a4622600.html

相关推荐

  • javascript - Audio tag, how to handle it from Angular - Stack Overflow

    I am using the HTML5 audio tag, but I want to control it from Angular, according to the guidelines I do

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信