Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS switch stylesheets based on user input

How can I switch / toggle the stylesheet of an AngularJS page based on a button click by the user?

like image 242
ozandlb Avatar asked May 13 '13 03:05

ozandlb


1 Answers

You can actually place a controller at the html level and modify the link tag's href:

Demo

Controller:

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';

  };
});

Markup:

<!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>
like image 100
lucuma Avatar answered Sep 19 '22 18:09

lucuma