Matthew Merryfull Matthew Merryfull - 1 year ago 160
CSS Question

Setting platform specific icons in Ionic

Okay, so this might be either a really simple question and I'll come away feeling sheepish but I've just gotten started using Ionic and I'm just playing around with detailing the UI with icons.

My question is whether or not you can set the icons in an element and have them render differently depending on which platform you build for?

For example, I had a menu item like so

<ion-content scroll="false">
<ul class="list">
<li class="item"><a href="#/login"><i class="icon ion-locked">Login</a></li>

In Ionicons, there is a
icon for both android (
) and ios (
)and I was wondering if there is a way to set icons to match their platform during the build process?

It may just be that I end up having seperate branches in git for this, but I'd rather not if there is a simple way to implement this?

Answer Source

Since Ionic Framework is built on Apache Cordova you can make use of the Apache Cordova Device plugin which ships standard with new projects:

You can then do something like this:

yourApp.controller("ExampleController", function($scope) {
   $scope.devicePlatform = device.platform;

Then in your HTML you can reference the controller and show the icon based on the platform:

<i ng-if="devicePlatform === 'Android' class="icon ion-android-locked">
<i ng-if="devicePlatform !== 'Android' class="icon ion-ios-locked">

Notice the ng-if. Depending on what the $scope.devicePlatform is will decide which of the tags will show.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download