AngularJS根据用户输入切换样式表

AngularJS switch stylesheets based on user input

本文关键字:样式 输入 用户 AngularJS      更新时间:2023-09-26

如何根据用户单击的按钮切换/切换AngularJS页面的样式表?

你实际上可以在html级别放置一个控制器并修改link标签的href:

控制器:

var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
});
app.controller('headController', function($scope) {
   $scope.stylePath = 'style.css'
   $scope.changePath = function() {
    $scope.stylePath='style2.css';
  };
});

标记:

<!doctype html>
<html ng-app="plunker" ng-controller='headController' >
<head >
  <meta charset="utf-8">
  <title>AngularJS Plunker</title>
  <script>document.write('<base href="' + document.location + '" />');</script>
  <link rel="stylesheet" href="{{stylePath}}">
  <script src="http://code.angularjs.org/1.1.4/angular.js"></script>
  <script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
  Hello {{name}}!
  <button ng-click="changePath()">Change</button>
</body>
</html>