User Input Based AngularJS Switch Style Sheets

How can I switch / switch the AngularJS page stylesheet based on user click?

+6
source share
1 answer

In fact, you can place the controller at the html level and change the link href tag:

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> 
+16
source

Source: https://habr.com/ru/post/944838/


All Articles