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

时间:2013-05-13 03:08:33

标签: javascript angularjs

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

1 个答案:

答案 0 :(得分:17)

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

<强> Demo

控制器:

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>