How can we switch the theme in Ionic App from Ligh

2020-03-26 17:36发布

问题:

I have two CSS file generated, One for lighter Version and other for darker Version. So please let me know how can i maintain two theme for same App?

Any idea how can we implement it on ionic app using theme toggled?

回答1:

Working plunker

You can easily toggle between CSS files.

index.html:

  1. set your ng-app on the HTML tag:

    <html ng-app="starter">
    
  2. dynamically set your style:

    <link ng-href="{{style}}" rel="stylesheet">
    

app.js: define your default style in run:

$rootScope.style = "css/style.css";

controllers.js: add your toggle style logic:

//All available styles
$scope.styles = [
  {
    name: "Default",
    url: "style.css"
  },
  {
    name: "Dark",
    url: "dark-style.css"
  }
];

/**
 * Change the style.
 * @param style The selected style.
 */
$scope.changeStyle = function(style){
  $rootScope.style = "css/" + style.url;
};

view.html: attach the related HTML:

<label class="item item-input item-select">
  <div class="input-label">
    Style
  </div>
  <select ng-options="style as style.name for style in styles" ng-change="changeStyle(style)" ng-model="style">
  </select>
</label>