Click here to Skip to main content
15,888,454 members
Articles / AngularJs
Tip/Trick

Create an ON/OFF switch using Angular JS and FontAwesome

Rate me:
Please Sign up or sign in to vote.
4.82/5 (5 votes)
18 Dec 2014CPOL 57.8K   6   8
This tip shows how you can create a very simple on/off switch using Angular js and fontawesome.

Introduction

On/Off switch is a very common usecase in any admin interface or otherwise.Traditionally, we have used the many different jQuery plugins available for this purpose. When using AngularJs, we try and do everything the "Angular Way".

Though there are many directives already written for this purpose, I still preferred to create my own which believe me is super easy to make and later customize if need be.

FontAwesome

For those who aren't aware of FontAwesome, it gives you scalable vector icons that can instantly be customized — size, color, drop shadow, and anything that can be done with the power of CSS. Visit its github page to know more.

Let's Get Started

Assuming you have imported Angular Js and FontAwesome in your HTML, below is how my controller looks like:

Controller

JavaScript
angular.module('switchdemo', []).controller('DemoController', function($scope){
  
  $scope.init = function(){
    $scope.status = true;
  }
  
  $scope.changeStatus = function(){
    $scope.status = !$scope.status;
  }
  
})

HTML

HTML
body ng-app="switchdemo">
  <h1>On Off switch using Angular JS</h1>
  <div ng-controller="DemoController" ng-init="init()">
    <div class="well">
      <i class="fa fa-toggle-on active"
         ng-if="status == true" 
         ng-click="changeStatus();">
      </i>
      <i class="fa fa-toggle-on fa-rotate-180 inactive"
         ng-if="status == false"
         ng-click="changeStatus();">
      </i>
    </div>
    <pre>{{ status }}</pre>
  </div>
</body>

CSS

CSS
.active, .inactive {font-size:40px;cursor:pointer;}
.active, .inactive {font-size:40px;cursor:pointer;}
i.active { color: #5cb85c}
i.inactive {color: #d9534f}

Here is how this should look like once you are done...

Demo

I have created a demo on plunkr with this code, here is the link to it: http://plnkr.co/edit/Kc8M3enHJB7iwFRyGUDr?p=preview

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Media.Net
India India
<ng-web-dev />

Comments and Discussions

 
QuestionGood article Pin
rammanusani26-Mar-19 8:15
rammanusani26-Mar-19 8:15 
QuestionHow to put text inside element. Pin
Mich_9018-Oct-15 1:38
Mich_9018-Oct-15 1:38 
QuestionReusability Pin
Ertunç Efeoğlu18-Dec-14 22:51
Ertunç Efeoğlu18-Dec-14 22:51 
AnswerRe: Reusability Pin
Yasser S19-Dec-14 19:32
professionalYasser S19-Dec-14 19:32 
GeneralMy vote of 5 Pin
Carsten V2.018-Dec-14 22:42
Carsten V2.018-Dec-14 22:42 
QuestionImages Missing Pin
syed shanu18-Dec-14 21:40
mvasyed shanu18-Dec-14 21:40 
AnswerRe: Images Missing Pin
Carsten V2.018-Dec-14 22:46
Carsten V2.018-Dec-14 22:46 
Hi syed,

Have you overlooked the link?
It's only a tip and not an article. So I guess there is no need of an image. Wink | ;)
But this is only my personal opinion.
AnswerRe: Images Missing Pin
Yasser S19-Dec-14 19:32
professionalYasser S19-Dec-14 19:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.