passing Scope value to javascript function

Asked

Viewed 536 times

1

How to pass a $scope.video_url for a javascript function in my view?

I have a service that returns the id of a video, and in my view I have a javascript infunction that starts the youtube player according to this id, but how do I pass this id to the function?

created a service, but no video appeared in my view.

myApp.service('youtubeAPI',function(){
    return({
        showMovie:movieYoutube,
    });

    function movieYoutube( video_url ){
        var player, iframe;

        // init player
        function onYouTubeIframeAPIReady() {
            player = new YT.Player('player', {
              height: '200',
              width: '342',
              videoId: video_url,
              events: {
                'onReady': onPlayerReady,
              }
            });
        }

        // when ready, wait for clicks
        function onPlayerReady(event) {
            var player = event.target;         
            player.playVideo();
        }
    }
});
  • You should consider creating an angular service and not putting javascript in your view. But since that doesn’t answer your question....

  • i can use the youtube api by creating a service?

  • The chance is that there should already be an implementation for what Voce wants. https://github.com/cejast/ng-youtube. And I gave you the wrong tip. In case you mess with the view the correct one is to use a Directive to manipulate what is needed in the DOM and communicate with the information that exists in the view and a Service to communicate with the external service that you are using. Finally, Directive uses the service to get the necessary data and put it in the view. Directive also processes the actions the user takes in the view relating to this Directive.

  • According to the service you want me to assemble, can you give me an example, this Directive thing has not yet entered my head? I understood the concept, that Directive takes the hints of the view and the service works on the external. In this service I set up, it cannot initialize the class YT.Player he accuses YT.Player is not definied

  • You have to see where and how you are including this YT library.

  • Take a look at this link that I gave you with an example of how to use youtube at the angle. It uses a Service to load the youtube script.

  • Vinicius, I’m including the YT library directly in the service.

Show 2 more comments

1 answer

1

You can use the angular inside your script in the view directly because it is globally defined.

<div ng-controller="AlgumCtrl" id="ctrlID"></div>
<script>
    function foo(){
        var scope = angular.element('#ctrlID').scope();
        var video_url = scope.video_url;
        //faça algo
    }
 </script>
  • this will get the Scope that I set in the div or all controller Scopes?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.