我正在尝试用Angularjs创建一个服务,以利用包括YouTube在内的各种oEmbed提供者。
...
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 },
})
});
...
URL结构为http://www.youtube.com/oembed?url=<url_of_video>
如何使此服务与用户提供的任何YouTube URL一起工作?换句话说,我可以从我的控制器调用这个服务并以某种方式传入URL吗?
YouTubeService.query(<url here maybe>)
发布于 2014-05-30 18:21:26
给你,我想这应该行得通。
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;
});
Invoke:
YouTubeService.query(<url here>)
发布于 2014-05-30 18:21:04
我不确定你是否可以像这样访问外部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);
https://stackoverflow.com/questions/23952104
复制相似问题