是否可以在Angular 2中动态更改全局样式表?

Thi*_*ibs 7 javascript angularjs angular

是否可以动态更改全局样式表?

编辑:目的是让用户选择他喜欢的样式.

在Angular 1中,我能够在头标签周围包裹一个控制器,并在那里使用绑定.

以下示例(简化代码):

的index.html

<!DOCTYPE html>
<html ng-app="app" ng-controller="AppController">
<head>
    <title>Title</title>
    <link rel="stylesheet" ng-href="styles/{{current}}"/>
</head>
...
Run Code Online (Sandbox Code Playgroud)

AppController的

app.controller('AppController', ['$scope', function ($scope ) {
    $scope.current = dynamicValue;
}]);
Run Code Online (Sandbox Code Playgroud)

这在Angular 2中是否可行?

Thi*_*ibs 11

我最终使用了DOCIGENT令牌,就像Igor在这里提到的那样.

从那里,我能够交换风格的href.

HTML:

<head>
   <link id="theme" rel="stylesheet" href="red.css">
</head>  
Run Code Online (Sandbox Code Playgroud)

TS:

import { Component, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';

@Component({})
export class MyClass {
    constructor (@Inject(DOCUMENT) private document) { }

    ngOnInit() {
      this.document.getElementById('theme').setAttribute('href', 'blue.css');
    }
}
Run Code Online (Sandbox Code Playgroud)