0

So I basically copied and pasted the code from this

http://plnkr.co/edit/0N728e9SAtXg3uBvuXKF?p=preview

To embed a youtube video.

It all works well, but now I'm trying to embed a youtube video where the "youtube video ID" is retrieved via service.

So in my controller, I do something like

  APIService.getVideo($stateParams.videoId).then(function(video) {
    $scope.code = video.youtube_video_id;
  });

And in my template, I have

<div my-youtube code="code"></div>

But this is not working.

Basically, in the directive,

app.directive('myYoutube', function($sce) {
  return {
    restrict: 'EA',
    scope: { code:'=' },
    replace: true,
    template: '<div style="height:400px;"><iframe style="overflow:hidden;height:100%;width:100%" width="100%" height="100%" src="{{url}}" frameborder="0" allowfullscreen></iframe></div>',
    link: function (scope) {
      scope.$watch('code', function (newVal) {
        if (newVal) {
          scope.url = $sce.trustAsResourceUrl("http://www.youtube.com/embed/" + newVal);
        }
      });
    }
  };
});

where there is src="{{url}}" I get "localhost/myapp/{{url}}" does not exist.

How do I get around this?

1 Answer 1

1

I've updated the plnkr you linked to include getting the code from a service. http://plnkr.co/edit/BggETGi7zAWL5bdHd0hO?p=preview

Here is my service, which basically mocks a server call for a code

app.factory('myFactory', function($q) {
  return {
    getCode: function() {
      var def = $q.defer();
      setTimeout(function() {
        def.resolve('oHg5SJYRHA0');
      }, 2000);
      return def.promise;
    }
  }
});

Hope this helps! Otherwise it would be helpful to get a broken plnkr to work off of

Sign up to request clarification or add additional context in comments.

2 Comments

hm, so what's actually going on here? I have to delay the returning of the data so that it loads properly? seems a very 'hacky'...... no?
the timeout is just there to simulate a server call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.