/
githubmirror
/
components
Обзор
Документация
Войти
/
githubmirror
/
components
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/src/app/shared/style-manager/style-manager.ts
50 строк
1 KB
Kristiyan Kostadinov
build: switch docs to service decorator
25 апр 2026, 23:10
25 апр 2026, 23:10
7ac0542
Код
Авторство
О чём код?
/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ import {Service} from '@angular/core'; /** * Class for managing stylesheets. Stylesheets are loaded into named slots so that they can be * removed or changed later. */ @Service() export class StyleManager { /** * Set the stylesheet with the specified key. */ setStyle(key: string, href: string) { getLinkElementForKey(key).setAttribute('href', href); } /** * Remove the stylesheet with the specified key. */ removeStyle(key: string) { getExistingLinkElementByKey(key)?.remove(); } } function getLinkElementForKey(key: string) { return getExistingLinkElementByKey(key) || createLinkElementWithKey(key); } function getExistingLinkElementByKey(key: string) { return document.head.querySelector(`link[rel="stylesheet"].${getClassNameForKey(key)}`); } function createLinkElementWithKey(key: string) { const linkEl = document.createElement('link'); linkEl.setAttribute('rel', 'stylesheet'); linkEl.classList.add(getClassNameForKey(key)); document.head.appendChild(linkEl); return linkEl; } function getClassNameForKey(key: string) { return `style-manager-${key}`; }