{"id":406955,"date":"2024-06-29T19:15:02","date_gmt":"2024-06-29T19:15:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=406955"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=406955","title":{"rendered":"<span>\u041b\u0435\u043d\u0438\u0432\u0430\u044f \u043f\u043e\u0434\u0433\u0440\u0443\u0437\u043a\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u0438\u0437 CDN \u0432 Angular<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u043e\u0433\u0434\u0430 \u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u043b \u0441\u0432\u043e\u0435 <a href=\"https:\/\/jamigo.app\/custom\/LZdhtPriojIoY0X38M3w\"><u>Angular-\u043a\u0430\u0440\u0430\u043e\u043a\u0435<\/u><\/a> \u0441 YouTube, \u043c\u043d\u0435 \u043f\u043e\u043f\u0430\u043b\u0441\u044f <a href=\"https:\/\/github.com\/angular\/components\/tree\/master\/src\/youtube-player#readme\"><u>\u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 YouTube-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442<\/u><\/a> \u0438\u0437 Angular Material. \u0412 README \u043f\u0440\u0438\u043b\u0430\u0433\u0430\u043b\u0430\u0441\u044c \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"dart\">let apiLoaded = false;  @Component({   template: '&lt;youtube-player videoId=\"PRQCAL_RMVo\">&lt;\/youtube-player>',   selector: 'youtube-player-example', }) class YoutubePlayerExample implements OnInit {   ngOnInit() {     if (!apiLoaded) {       const tag = document.createElement('script');       tag.src = 'https:\/\/www.youtube.com\/iframe_api';       document.body.appendChild(tag);       apiLoaded = true;     }   } } <\/code><\/pre>\n<p>\u041f\u043e\u0447\u0442\u0438 \u043a\u0430\u0436\u0434\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0432 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0441\u043e\u043c\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430 \u2014 \u043e\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u0440\u0430\u043d\u043d\u0435\u0433\u043e \u0432\u044b\u0445\u043e\u0434\u0430 \u0434\u043e \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0438 \u043f\u0440\u044f\u043c\u043e\u0439 \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0438 \u0441 DOM. \u042d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043d\u043e \u0432 Angular \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0443\u0447\u0448\u0435! \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u043c\u0435\u044e\u0449\u0438\u0435\u0441\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u043b\u0435\u043d\u0438\u0432\u043e\u0439 \u043f\u043e\u0434\u0433\u0440\u0443\u0437\u043a\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e46\/fda\/33b\/e46fda33b298aa2750ca9d5a017c09a3.png\" width=\"1561\" height=\"881\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e46\/fda\/33b\/e46fda33b298aa2750ca9d5a017c09a3.png\"\/><figcaption><\/figcaption><\/figure>\n<h2>Dependency Injection \u0441\u043d\u043e\u0432\u0430 \u0432\u044b\u0440\u0443\u0447\u0430\u0435\u0442<\/h2>\n<p>\u0415\u0441\u0442\u044c \u043d\u0435\u043f\u043b\u043e\u0445\u043e\u0439 \u0442\u0440\u044e\u043a \u0434\u043b\u044f \u043b\u0435\u043d\u0438\u0432\u043e\u0439 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0431\u043b\u043e\u043a\u0430 \u043a\u043e\u0434\u0430 \u0447\u0435\u0440\u0435\u0437 \u0442\u043e\u043a\u0435\u043d, \u043f\u0440\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0438\u0441\u0430\u043b <a href=\"https:\/\/twitter.com\/thekiba_io\"><u>Reactive Fox<\/u><\/a><u>:<\/u>\u00a0<\/p>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/6177c4c1f61f533babdf2ef4\" data-style=\"\" id=\"6177c4c1f61f533babdf2ef4\" width=\"\"><\/div>\n<p>\u0423 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0435\u0441\u0442\u044c \u043e\u0434\u0438\u043d \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u2014 \u043e\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0444\u0430\u0439\u043b\u0430\u043c\u0438. \u0422\u0430\u043a \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0441 CDN. \u041d\u043e \u043c\u044b \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0442\u043e\u043a\u0435\u043d \u0434\u043b\u044f \u043f\u043e\u0445\u043e\u0436\u0435\u0433\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0430.\u00a0<\/p>\n<p>DI-\u0442\u043e\u043a\u0435\u043d \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u042d\u0442\u043e \u0445\u043e\u0440\u043e\u0448\u0430\u044f \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430 \u0433\u0440\u044f\u0437\u043d\u043e\u043c\u0443 \u043f\u043e\u0434\u0445\u043e\u0434\u0443 \u0441 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u044b\u0448\u0435. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u043e\u043a\u0435\u043d\u0430 <code>DOCUMENT<\/code> \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u043f\u0440\u044f\u043c\u043e\u0439 \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0438 DOM, \u0438 \u043d\u0430\u0448 \u043a\u043e\u0434 \u043d\u0435 \u0443\u043f\u0430\u0434\u0435\u0442 \u043f\u0440\u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u043c \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u043d\u0433\u0435 (<a href=\"https:\/\/habr.com\/ru\/company\/tinkoff\/blog\/548510\/\"><u>\u043e\u0431 \u044d\u0442\u043e\u043c \u044f \u0443\u0436\u0435 \u043f\u0438\u0441\u0430\u043b<\/u><\/a>). \u041d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u043d\u0430 CDN URL:<\/p>\n<pre><code class=\"dart\">export const API_URL = new InjectionToken&lt;string>('CDN URL of Youtube API', {   factory: () => 'https:\/\/www.youtube.com\/iframe_api' });<\/code><\/pre>\n<p>\u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0447\u0430\u0441\u0442\u043e \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u044e\u0442 \u0441\u0435\u0431\u044f \u043d\u0430 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0435 <code>window<\/code>. \u0412\u043e\u0442 \u0442\u0430\u043a \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0438\u043f\u043e\u0432\u043e\u0439 \u0442\u043e\u043a\u0435\u043d \u043d\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443:<\/p>\n<pre><code class=\"dart\">export const API$ = new InjectionToken&lt;Observable&lt;Interface>>(   'A stream with third party library object',   {     factory: () => {       const documentRef = inject(DOCUMENT);       const script = documentRef.createElement('script');        script.src = inject(API_URL);       documentRef.body.appendChild(script);        return fromEvent(script, 'load').pipe(         map(() => documentRef.defaultView.libraryObject),       );     }   } );<\/code><\/pre>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435 YouTube API \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0436\u0434\u0430\u0442\u044c\u0441\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0441\u043a\u0440\u0438\u043f\u0442\u0430. <a href=\"https:\/\/developers.google.com\/youtube\/iframe_api_reference\"><u>\u0412 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/u><\/a> \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u043b\u0431\u044d\u043a <code>onYouTubeIframeAPIReady<\/code> \u043d\u0430 <code>window<\/code> \u0438 \u0441\u043a\u0440\u0438\u043f\u0442 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u0435\u0433\u043e, \u043a\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0433\u043e\u0442\u043e\u0432. \u0421\u0442\u043e\u0438\u0442 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442 \u0432 \u043e\u0431\u0445\u043e\u0434 zone.js. \u0422\u043e \u0435\u0441\u0442\u044c, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043c\u044b \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043e\u0442\u0440\u0435\u0430\u0433\u0438\u0440\u0443\u0435\u043c \u043d\u0430 \u0432\u044b\u0437\u043e\u0432, Angular \u0435\u0433\u043e \u043d\u0435 \u0437\u0430\u043c\u0435\u0442\u0438\u0442 \u0438 \u043d\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0432 <code>NgZone.run<\/code>:<\/p>\n<pre><code class=\"dart\">export const API$ = new InjectionToken&lt;Observable&lt;YT>>(   'A stream with YT object',   {     factory: () => {       const documentRef = inject(DOCUMENT);       const zone = inject(NgZone);       const windowRef = documentRef.defaultView;       const script = documentRef.createElement('script');       const loaded$ = new ReplaySubject(1);        script.src = inject(API_URL);       documentRef.body.appendChild(script);        windowRef.onYouTubeIframeAPIReady = () => {         zone.run(() => loaded$.next(windowRef.YT));       };        return loaded$;     }   } );<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0442\u043e\u043a\u0435\u043d \u2014 <code>Observable<\/code>. \u0412 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0442\u043e\u043a\u0435\u043d \u043d\u0430 \u0441\u0430\u043c \u043e\u0431\u044a\u0435\u043a\u0442 <code>YT<\/code>:<\/p>\n<pre><code class=\"dart\">export const API = new InjectionToken&lt;YT>('Youtube API object');<\/code><\/pre>\n<h2>\u0420\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u043c<\/h2>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c <code>Observable<\/code> \u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0438\u043c \u0434\u0430\u043b\u044c\u0448\u0435 \u2014 <a href=\"https:\/\/angular.io\/api\/router\/Resolve\"><u>Resolver.<\/u><\/a> \u0412\u043e\u0437\u044c\u043c\u0435\u043c lazy-\u0440\u043e\u0443\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 YouTube, \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0441\u043e\u043b\u0432\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u044c\u043c\u0435\u0442 \u043d\u0430\u0448 \u0442\u043e\u043a\u0435\u043d \u0438 \u0434\u043e\u0436\u0434\u0435\u0442\u0441\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438. \u0422\u0430\u043a \u043e\u0431\u044a\u0435\u043a\u0442 <code>YT<\/code> \u0441\u0442\u0430\u043d\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u043c \u0432 <code>data<\/code> \u043d\u0430\u0448\u0435\u0433\u043e \u0440\u043e\u0443\u0442\u0430. \u0417\u0430\u0442\u0435\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u0442\u043e\u043a\u0435\u043d. \u0427\u0442\u043e-\u0442\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0435 <a href=\"https:\/\/twitter.com\/HienHuuPham\"><u>Hien Pham<\/u><\/a> \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043b \u0432 \u0441\u0432\u043e\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/indepth.dev\/posts\/1471\/leveraging-dependency-injection-to-reduce-duplicated-code-in-angular\"><u>\u043f\u0440\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 DI.<\/u><\/a><\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u2014 \u0441\u043e\u0437\u0434\u0430\u0442\u044c <a href=\"https:\/\/angular.io\/guide\/structural-directives\"><u>\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u0443<\/u><\/a>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0436\u0434\u0430\u0442\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 API, \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u0438\u043d\u0441\u0442\u0430\u043d\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d. \u0421\u0430\u043c\u0430 \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u0430 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0439:<\/p>\n<pre><code class=\"dart\">export class WithYTDirective implements OnChanges {   @Input()   withYT: YT | null = null;    constructor(     private readonly templateRef: TemplateRef&lt;{}>,     private readonly vcr: ViewContainerRef   ) {}    ngOnChanges() {     if (this.withYT) {       this.vcr.createEmbeddedView(this.templateRef);     }   } }<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0448\u0430\u0431\u043b\u043e\u043d \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0433\u0434\u0430 \u0432 <code>withYT<\/code> \u0443\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 <code>YT<\/code>, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440:<\/p>\n<pre><code class=\"dart\">export function extractAPI({ withYT }: WithYTDirective): YT {   return withYT; }  @Directive({   selector: '[withYT]',   providers: [     {       provide: API,       deps: [WithYTDirective],       useFactory: extractAPI     }   ] }) export class WithYTDirective implements OnChanges {   \/\/ ... }<\/code><\/pre>\n<blockquote>\n<p><em>\u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u0444\u0430\u0431\u0440\u0438\u043a\u0430 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430 \u0432\u044b\u0437\u043e\u0432\u0435\u0442\u0441\u044f, \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0433\u0434\u0430 \u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u044f\u0442.<\/em><\/p>\n<\/blockquote>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 YouTube, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e \u0431\u0435\u0440\u0435\u0442 API \u0438\u0437 DI. \u041e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u044f \u043d\u0435 \u0431\u0443\u0434\u0443. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043e\u043d \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c. \u0412\u043e\u0442 \u0442\u0430\u043a \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0441 \u043d\u0430\u0448\u0435\u0439 \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u043e\u0439:<\/p>\n<pre><code class=\"dart\">@Component(   selector: 'my-component',   template: `&lt;youtube-player *withYT=\"api$ | async\">&lt;\/youtube-player>` ) export class MyComponent {   constructor(@Inject(API$) readonly api$: Observable&lt;YT>) {} }<\/code><\/pre>\n<p>\u0418\u0442\u043e\u0433\u043e\u0432\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"https:\/\/stackblitz.com\/edit\/angular-di-youtube\"><u>\u043d\u0430 StackBlitz.<\/u><\/a><\/p>\n<h2>\u0412\u043c\u0435\u0441\u0442\u043e \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f<\/h2>\n<p>DI, \u0442\u043e\u043a\u0435\u043d\u044b \u0438 \u0444\u0430\u0431\u0440\u0438\u043a\u0438 \u0434\u0430\u044e\u0442 \u043c\u043e\u0449\u043d\u044b\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0432 \u043d\u0438\u0445 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u0438 \u043b\u043e\u0433\u0438\u043a\u0443 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u042d\u0442\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a, \u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0438 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439.\u00a0<\/p>\n<p>\u0423\u0434\u0435\u043b\u0438\u0442\u0435 \u043f\u043e\u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0447\u0442\u043e\u0431\u044b \u0431\u043b\u0438\u0436\u0435 \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 Dependency Injection, \u0438 \u043e\u043d \u0441\u0442\u0430\u043d\u0435\u0442 \u0432\u0430\u0448\u0438\u043c \u0432\u0435\u0440\u043d\u044b\u043c \u0441\u043e\u044e\u0437\u043d\u0438\u043a\u043e\u043c \u0432 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u043d\u0430\u0434\u0435\u0436\u043d\u043e\u0433\u043e \u0433\u0438\u0431\u043a\u043e\u0433\u043e \u043a\u043e\u0434\u0430.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/585534\/\"> https:\/\/habr.com\/ru\/articles\/585534\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u043e\u0433\u0434\u0430 \u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u043b \u0441\u0432\u043e\u0435 <a href=\"https:\/\/jamigo.app\/custom\/LZdhtPriojIoY0X38M3w\"><u>Angular-\u043a\u0430\u0440\u0430\u043e\u043a\u0435<\/u><\/a> \u0441 YouTube, \u043c\u043d\u0435 \u043f\u043e\u043f\u0430\u043b\u0441\u044f <a href=\"https:\/\/github.com\/angular\/components\/tree\/master\/src\/youtube-player#readme\"><u>\u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 YouTube-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442<\/u><\/a> \u0438\u0437 Angular Material. \u0412 README \u043f\u0440\u0438\u043b\u0430\u0433\u0430\u043b\u0430\u0441\u044c \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"dart\">let apiLoaded = false;  @Component({   template: '&lt;youtube-player videoId=\"PRQCAL_RMVo\">&lt;\/youtube-player>',   selector: 'youtube-player-example', }) class YoutubePlayerExample implements OnInit {   ngOnInit() {     if (!apiLoaded) {       const tag = document.createElement('script');       tag.src = 'https:\/\/www.youtube.com\/iframe_api';       document.body.appendChild(tag);       apiLoaded = true;     }   } } <\/code><\/pre>\n<p>\u041f\u043e\u0447\u0442\u0438 \u043a\u0430\u0436\u0434\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0432 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0441\u043e\u043c\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430 \u2014 \u043e\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u0440\u0430\u043d\u043d\u0435\u0433\u043e \u0432\u044b\u0445\u043e\u0434\u0430 \u0434\u043e \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0438 \u043f\u0440\u044f\u043c\u043e\u0439 \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0438 \u0441 DOM. \u042d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043d\u043e \u0432 Angular \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0443\u0447\u0448\u0435! \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u043c\u0435\u044e\u0449\u0438\u0435\u0441\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u043b\u0435\u043d\u0438\u0432\u043e\u0439 \u043f\u043e\u0434\u0433\u0440\u0443\u0437\u043a\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h2>Dependency Injection \u0441\u043d\u043e\u0432\u0430 \u0432\u044b\u0440\u0443\u0447\u0430\u0435\u0442<\/h2>\n<p>\u0415\u0441\u0442\u044c \u043d\u0435\u043f\u043b\u043e\u0445\u043e\u0439 \u0442\u0440\u044e\u043a \u0434\u043b\u044f \u043b\u0435\u043d\u0438\u0432\u043e\u0439 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0431\u043b\u043e\u043a\u0430 \u043a\u043e\u0434\u0430 \u0447\u0435\u0440\u0435\u0437 \u0442\u043e\u043a\u0435\u043d, \u043f\u0440\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0438\u0441\u0430\u043b <a href=\"https:\/\/twitter.com\/thekiba_io\"><u>Reactive Fox<\/u><\/a><u>:<\/u>\u00a0<\/p>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/6177c4c1f61f533babdf2ef4\" data-style=\"\" id=\"6177c4c1f61f533babdf2ef4\" width=\"\"><\/div>\n<p>\u0423 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0435\u0441\u0442\u044c \u043e\u0434\u0438\u043d \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u2014 \u043e\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0444\u0430\u0439\u043b\u0430\u043c\u0438. \u0422\u0430\u043a \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0441 CDN. \u041d\u043e \u043c\u044b \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0442\u043e\u043a\u0435\u043d \u0434\u043b\u044f \u043f\u043e\u0445\u043e\u0436\u0435\u0433\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0430.\u00a0<\/p>\n<p>DI-\u0442\u043e\u043a\u0435\u043d \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u042d\u0442\u043e \u0445\u043e\u0440\u043e\u0448\u0430\u044f \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430 \u0433\u0440\u044f\u0437\u043d\u043e\u043c\u0443 \u043f\u043e\u0434\u0445\u043e\u0434\u0443 \u0441 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u044b\u0448\u0435. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u043e\u043a\u0435\u043d\u0430 <code>DOCUMENT<\/code> \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u043f\u0440\u044f\u043c\u043e\u0439 \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0438 DOM, \u0438 \u043d\u0430\u0448 \u043a\u043e\u0434 \u043d\u0435 \u0443\u043f\u0430\u0434\u0435\u0442 \u043f\u0440\u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u043c \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u043d\u0433\u0435 (<a href=\"https:\/\/habr.com\/ru\/company\/tinkoff\/blog\/548510\/\"><u>\u043e\u0431 \u044d\u0442\u043e\u043c \u044f \u0443\u0436\u0435 \u043f\u0438\u0441\u0430\u043b<\/u><\/a>). \u041d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u043d\u0430 CDN URL:<\/p>\n<pre><code class=\"dart\">export const API_URL = new InjectionToken&lt;string>('CDN URL of Youtube API', {   factory: () => 'https:\/\/www.youtube.com\/iframe_api' });<\/code><\/pre>\n<p>\u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0447\u0430\u0441\u0442\u043e \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u044e\u0442 \u0441\u0435\u0431\u044f \u043d\u0430 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0435 <code>window<\/code>. \u0412\u043e\u0442 \u0442\u0430\u043a \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0438\u043f\u043e\u0432\u043e\u0439 \u0442\u043e\u043a\u0435\u043d \u043d\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443:<\/p>\n<pre><code class=\"dart\">export const API$ = new InjectionToken&lt;Observable&lt;Interface>>(   'A stream with third party library object',   {     factory: () => {       const documentRef = inject(DOCUMENT);       const script = documentRef.createElement('script');        script.src = inject(API_URL);       documentRef.body.appendChild(script);        return fromEvent(script, 'load').pipe(         map(() => documentRef.defaultView.libraryObject),       );     }   } );<\/code><\/pre>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435 YouTube API \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0436\u0434\u0430\u0442\u044c\u0441\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0441\u043a\u0440\u0438\u043f\u0442\u0430. <a href=\"https:\/\/developers.google.com\/youtube\/iframe_api_reference\"><u>\u0412 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/u><\/a> \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u043b\u0431\u044d\u043a <code>onYouTubeIframeAPIReady<\/code> \u043d\u0430 <code>window<\/code> \u0438 \u0441\u043a\u0440\u0438\u043f\u0442 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u0435\u0433\u043e, \u043a\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0433\u043e\u0442\u043e\u0432. \u0421\u0442\u043e\u0438\u0442 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442 \u0432 \u043e\u0431\u0445\u043e\u0434 zone.js. \u0422\u043e \u0435\u0441\u0442\u044c, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043c\u044b \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043e\u0442\u0440\u0435\u0430\u0433\u0438\u0440\u0443\u0435\u043c \u043d\u0430 \u0432\u044b\u0437\u043e\u0432, Angular \u0435\u0433\u043e \u043d\u0435 \u0437\u0430\u043c\u0435\u0442\u0438\u0442 \u0438 \u043d\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0432 <code>NgZone.run<\/code>:<\/p>\n<pre><code class=\"dart\">export const API$ = new InjectionToken&lt;Observable&lt;YT>>(   'A stream with YT object',   {     factory: () => {       const documentRef = inject(DOCUMENT);       const zone = inject(NgZone);       const windowRef = documentRef.defaultView;       const script = documentRef.createElement('script');       const loaded$ = new ReplaySubject(1);        script.src = inject(API_URL);       documentRef.body.appendChild(script);        windowRef.onYouTubeIframeAPIReady = () => {         zone.run(() => loaded$.next(windowRef.YT));       };        return loaded$;     }   } );<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0442\u043e\u043a\u0435\u043d \u2014 <code>Observable<\/code>. \u0412 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0442\u043e\u043a\u0435\u043d \u043d\u0430 \u0441\u0430\u043c \u043e\u0431\u044a\u0435\u043a\u0442 <code>YT<\/code>:<\/p>\n<pre><code class=\"dart\">export const API = new InjectionToken&lt;YT>('Youtube API object');<\/code><\/pre>\n<h2>\u0420\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u043c<\/h2>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c <code>Observable<\/code> \u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0438\u043c \u0434\u0430\u043b\u044c\u0448\u0435 \u2014 <a href=\"https:\/\/angular.io\/api\/router\/Resolve\"><u>Resolver.<\/u><\/a> \u0412\u043e\u0437\u044c\u043c\u0435\u043c lazy-\u0440\u043e\u0443\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 YouTube, \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0441\u043e\u043b\u0432\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u044c\u043c\u0435\u0442 \u043d\u0430\u0448 \u0442\u043e\u043a\u0435\u043d \u0438 \u0434\u043e\u0436\u0434\u0435\u0442\u0441\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438. \u0422\u0430\u043a \u043e\u0431\u044a\u0435\u043a\u0442 <code>YT<\/code> \u0441\u0442\u0430\u043d\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u043c \u0432 <code>data<\/code> \u043d\u0430\u0448\u0435\u0433\u043e \u0440\u043e\u0443\u0442\u0430. \u0417\u0430\u0442\u0435\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u0442\u043e\u043a\u0435\u043d. \u0427\u0442\u043e-\u0442\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0435 <a href=\"https:\/\/twitter.com\/HienHuuPham\"><u>Hien Pham<\/u><\/a> \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043b \u0432 \u0441\u0432\u043e\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/indepth.dev\/posts\/1471\/leveraging-dependency-injection-to-reduce-duplicated-code-in-angular\"><u>\u043f\u0440\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 DI.<\/u><\/a><\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u2014 \u0441\u043e\u0437\u0434\u0430\u0442\u044c <a href=\"https:\/\/angular.io\/guide\/structural-directives\"><u>\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u0443<\/u><\/a>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0436\u0434\u0430\u0442\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 API, \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u0438\u043d\u0441\u0442\u0430\u043d\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d. \u0421\u0430\u043c\u0430 \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u0430 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0439:<\/p>\n<pre><code class=\"dart\">export class WithYTDirective implements OnChanges {   @Input()   withYT: YT | null = null;    constructor(     private readonly templateRef: TemplateRef&lt;{}>,     private readonly vcr: ViewContainerRef   ) {}    ngOnChanges() {     if (this.withYT) {       this.vcr.createEmbeddedView(this.templateRef);     }   } }<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0448\u0430\u0431\u043b\u043e\u043d \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0433\u0434\u0430 \u0432 <code>withYT<\/code> \u0443\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 <code>YT<\/code>, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440:<\/p>\n<pre><code class=\"dart\">export function extractAPI({ withYT }: WithYTDirective): YT {   return withYT; }  @Directive({   selector: '[withYT]',   providers: [     {       provide: API,       deps: [WithYTDirective],       useFactory: extractAPI     }   ] }) export class WithYTDirective implements OnChanges {   \/\/ ... }<\/code><\/pre>\n<blockquote>\n<p><em>\u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u0444\u0430\u0431\u0440\u0438\u043a\u0430 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430 \u0432\u044b\u0437\u043e\u0432\u0435\u0442\u0441\u044f, \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0433\u0434\u0430 \u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u044f\u0442.<\/em><\/p>\n<\/blockquote>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 YouTube, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e \u0431\u0435\u0440\u0435\u0442 API \u0438\u0437 DI. \u041e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u044f \u043d\u0435 \u0431\u0443\u0434\u0443. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043e\u043d \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c. \u0412\u043e\u0442 \u0442\u0430\u043a \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0441 \u043d\u0430\u0448\u0435\u0439 \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u043e\u0439:<\/p>\n<pre><code class=\"dart\">@Component(   selector: 'my-component',   template: `&lt;youtube-player *withYT=\"api$ | async\">&lt;\/youtube-player>` ) export class MyComponent {   constructor(@Inject(API$) readonly api$: Observable&lt;YT>) {} }<\/code><\/pre>\n<p>\u0418\u0442\u043e\u0433\u043e\u0432\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"https:\/\/stackblitz.com\/edit\/angular-di-youtube\"><u>\u043d\u0430 StackBlitz.<\/u><\/a><\/p>\n<h2>\u0412\u043c\u0435\u0441\u0442\u043e \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f<\/h2>\n<p>DI, \u0442\u043e\u043a\u0435\u043d\u044b \u0438 \u0444\u0430\u0431\u0440\u0438\u043a\u0438 \u0434\u0430\u044e\u0442 \u043c\u043e\u0449\u043d\u044b\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0432 \u043d\u0438\u0445 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u0438 \u043b\u043e\u0433\u0438\u043a\u0443 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u042d\u0442\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a, \u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0438 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439.\u00a0<\/p>\n<p>\u0423\u0434\u0435\u043b\u0438\u0442\u0435 \u043f\u043e\u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0447\u0442\u043e\u0431\u044b \u0431\u043b\u0438\u0436\u0435 \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 Dependency Injection, \u0438 \u043e\u043d \u0441\u0442\u0430\u043d\u0435\u0442 \u0432\u0430\u0448\u0438\u043c \u0432\u0435\u0440\u043d\u044b\u043c \u0441\u043e\u044e\u0437\u043d\u0438\u043a\u043e\u043c \u0432 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u043d\u0430\u0434\u0435\u0436\u043d\u043e\u0433\u043e \u0433\u0438\u0431\u043a\u043e\u0433\u043e \u043a\u043e\u0434\u0430.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/585534\/\"> https:\/\/habr.com\/ru\/articles\/585534\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-406955","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/406955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=406955"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/406955\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=406955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=406955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=406955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}