Reputation: 1024
How can I switch / toggle the stylesheet of an AngularJS page based on a button click by the user?
Upvotes: 6
Views: 4171
Reputation: 18339
You can actually place a controller at the html level and modify the link
tag's href:
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>
Upvotes: 17