How can I switch / toggle the stylesheet of an AngularJS page based on a button click by the user?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With