Angularjs YouTube oEmbed服务

Angularjs YouTube oEmbed服务,angularjs,Angularjs,我正在尝试用Angularjs创建一个服务,以利用各种oEmbed提供商,包括YouTube ... myServices.factory('YouTubeService', function ($resource) { //how can I make the URL part dynamic? return $resource('http://www.youtube.com/oembed/', {}, { query: { method: 'GET', isA

我正在尝试用Angularjs创建一个服务,以利用各种oEmbed提供商,包括YouTube

...
myServices.factory('YouTubeService', function ($resource) {
    //how can I make the URL part dynamic?
    return $resource('http://www.youtube.com/oembed/', {}, {
        query: { method: 'GET', isArray: true },
    })
});
...
oEmbed URL结构是
http://www.youtube.com/oembed?url=

如何使此服务与用户提供的任何YouTube URL一起工作?换句话说,我可以从控制器调用此服务并以某种方式传入URL吗

YouTubeService.query(<url here maybe>)
YouTubeService.query()

我不确定您是否可以像这样访问外部url(可能引发跨域错误)

但对于你的问题,你为什么不使用服务而不是像这样的工厂呢

myServices.service('YouTubeService', function ($resource) {
    //how can I make the URL part dynamic?
    this.getStuff = function(url){
       return $resource(url, {}, {
        query: { method: 'GET', isArray: true },
    }).query();
    }

});
然后像这样调用它

YouTubeService.getStuff (dynamicUrl);

给你,我想这应该管用

myServices.factory('YouTubeService', function ($resource) {
    var youtubeservice = {};

    youtubeservice.query = function(urlProvided){
       return $resource('http://www.youtube.com/oembed?url=:urlProvided', {}, {
         query: { method: 'GET', isArray: true },
       });
    }
    return youtubeservice;

});
调用:

YouTubeService.query(<url here>)
YouTubeService.query()

你说得对-收到一条
访问控制允许原点
错误消息。有什么想法吗?你需要$sce.trustAsResourceUrl