6

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

4

1 回答 1

17

您实际上可以在 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>
于 2013-05-13T03:20:59.213 回答