Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Angular 2 UI Switch - A Nice, Reusable Component!

1/14/2017

0 Comments

 
This is a great library that I like a lot: angular2-ui-switch. It's a clean toggle switch component reminiscent of old iPhone iOS 7 operating system. Here's a post on how to incorporate this library into your own Angular 2 projects. 

Step 1) Install The Library

First, we need to install this npm library:
 npm install angular2-ui-switch --save

Step 2) Declare UiSwitchModule In Your Module's Imports

Now, add the Class UiSwitchModule into the "imports" array of your @NgModule like this:
import {UiSwitchModule} from "angular2-ui-switch";

@NgModule({
  declarations: [  
  ],
  imports: [
    UiSwitchModule
  ],
  providers: [
  ],
  bootstrap: [AppComponent,
  ]
})
export class AppModule { }

Step 3) Use <ui-switch> In Your HTML

Once the library is properly set up you can use the <ui-switch> tag in your html template:

<ui-switch></ui-switch>

Step 4) Listen For Events In Component TypeScript Code When The Toggle Switch Is Pressed

Now that you have this fancy new toggle switch you'll probably want things in your application to happen when the user clicks to toggle it! First, we can add bindings to the click event and ngModel property in our ui-switch tag:
<ui-switch [(ngModel)]="toggleValue" (click)="toggleClicked()"></ui-switch>
Then in our component we can implement this function that gets triggered whenever the ui switch is clicked:
private toggleClicked () {
    console.log('the toggle has been clicked! ' + this.toggleValue);
}
Note that you could also listen for the change event since the value changes when this component is clicked:
<ui-switch (change)="onChange($event)"></ui-switch>

Decide How To Utilize This Awesome Toggle Switch!

I hope this post has convinced you how easy it is to add this sexy toggle slider into your Angular 2 project. At this point there's a new challenge; deciding how to incorporate these into your app. I'm using these right now in my profile, but I also have been known to put them in modals if I need to get a boolean value from the user. Hopefully your UI / UX people love these toggle switches and can put their creative minds to work replacing boring checkboxes and text inputs. Have fun (just don't go too crazy with the toggle switches hehe)!
Picture
Picture

Further Reference

You can find more information about the angular2-ui-switch library here:
  • npm page: https://www.npmjs.com/package/angular2-ui-switch
  • github page: https://github.com/yuyang041060120/angular2-ui-switch
  • demo page: http://yuyang041060120.github.io/angular2-ui-switch/
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Want FREE access to
    my daily stock tips 
    ​newsletters??
    Sign up here:

    - Triple Gainers
    - Rippers N' Dippers
    - Growingest Growers
    ​

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021