<!DOCTYPE html>
<html ng-app="app">
<head>
<link data-require="bootstrap@3.3.7" data-semver="3.3.7" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script data-require="bootstrap@3.3.7" data-semver="3.3.7" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script data-require="angular.js@1.6.0" data-semver="1.6.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.js"></script>
<link class="style-a" rel="stylesheet" href="style-a.css" />
<link class="style-b" rel="stylesheet" href="style-b.css" />
<link class="style-c" rel="stylesheet" href="style-c.css" />
<script src="script.js"></script>
</head>
<body ng-controller="mainController">
<button class="btn btn-default" ng-click="switch('style-a')">style-a.cssを適用</button>
<button class="btn btn-default" ng-click="switch('style-b')">style-b.cssを適用</button>
<button class="btn btn-default" ng-click="switch('style-c')">style-c.cssを適用</button>
<div class="box" style="width:100px; height:100px; margin: 20px;"></div>
</body>
</html>
// Code goes here
angular.module('app', [])
// 使用するスタイルシートの切り替えを行うサービス
.factory('cssSwitch', function() {
return {
enableCss: function enableCss(className) {
// 有効とするCSSのdisabled属性をはずす
$('link.' + className).removeAttr('disabled');
},
disableCss: function disableCss(className) {
// 無効とするCSSにdisabled属性をつける
$('link.' + className).attr('disabled', 'disabled');
}
};
})
.controller('mainController', function($scope, cssSwitch){
var classes = ['style-a', 'style-b', 'style-c'];
$scope.switch = function(name){
angular.forEach(classes, function(c){
cssSwitch.disableCss(c);
});
cssSwitch.enableCss(name);
};
})
.box {
border-radius: 25px;
background: #73AD21;
padding: 20px;
}
.box {
background-color: blue;
border-radius: 55px;
}
.box {
background-color: red;
}