Angular UI Components Documentation
Angular SwitchButton Component
The SwitchButton component for Angular lets the user toggle between checked and unchecked states.
Prerequisites
Refer to Angular Getting Started before you start with this help topic.
Configuration
I. Imports
The SwitchButton component for Angular requires the following imports.
II. @Component Decorator
A class is marked as an Angular component through the @Component decorator:
- selector - CSS selector that identifies this component in a template
- styles - inline-defined styles to be applied to this component's view
- styleUrls - list of URLs to stylesheets to be applied to this component's view
- template - inline-defined template for the view
- templateUrl - URL to an external file containing a template for the view
Every jQWidgets Angular component has an [auto-create] attribute which determines whether the component is automatically created or is created on demand by API method call.
By default, the [auto-create] attribute value is true.
III. Create the AppComponent class
The AppComponent class is used to instantiate the app. In some of the examples, we use @ViewChild and ngAfterViewInit.
-
@ViewChild: makes a reference using a SwitchButton selector.
View queries are set before the ngAfterViewInit callback is called.
@ViewChild('switchButtonReference') mySwitchButton: jqxSwitchButtonComponent;
-
ngAfterViewInit - this is an Angular Component Lifecycle method in which we create our angular components. After the view is initialized,
mySwitchButton
is available.
For more information about this methods please refer to Angular Component Lifecycle API
The following example demonstrates the SwitchButton
component for Angular.
app.component.htm
app.component.css
app.component.ts
IV. Create SwitchButton component with JSON
-
Add the imports.
import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { jqxSwitchButtonComponent } from 'jqwidgets-ng/jqxswitchbutton';
@Component decorator:
@Component({ selector: 'app-root', template: `<jqxSwitchButton #switchButtonReference></jqxSwitchButton>` })
-
Create the AppComponent class.
-
@ViewChild: makes a reference using a SwitchButton selector.
View queries are set before the ngAfterViewInit callback is called.
@ViewChild('switchButtonReference') mySwitchButton: jqxSwitchButtonComponent;
-
ngAfterViewInit: All Angular components have an integrated method called createComponent that accept a JSON object with the component initialization properties. If no argument is passed, default properties will be used. If you would like to dynamically set properties after creation, you can use the
setOptions
method.
ngAfterViewInit(): void { this.mySwitchButton.createComponent(this.SwitchButtonSettings); }
-
@ViewChild: makes a reference using a SwitchButton selector.
View queries are set before the ngAfterViewInit callback is called.
-
The following example demonstrates how to create a SwitchButton component for Angular by using the createComponent method.
Properties
The properties which are defined as attributes can be changed directly:
To get or set a property, which we did not define or call a method, we can do the following:
-
Import @ViewChild decorator:
-
Add the SwitchButton component:
-
Get access to the SwitchButton component and its API:
-
Finally, we can call methods or set/get properties like that:
Every component has a method setOptions
which accepts a JSON object as an argument.
This object contains component settings and you can use it to set a single or multiple properties dynamically.
Events
The change
event is triggered when the switch button's state changes from one state to another.
The following example demonstrates how to add an event listener.
The only thing you need to do is to put
"on"
before the event name and upperCase it's the first letter.
Methods
To invoke methods, you can use the SwitchButton reference.
( enable() to enable the widget ) - this.mySwitchButton.enable()
.
The following example demonstrates how to invoke methods of the SwitchButton
component for Angular.
SwitchButton Examples
Overview
The following example demonstrates how to create a SwitchButton
component for Angular.
Disabling
The following example demonstrates how to disable the SwitchButton
component for Angular.
Change ON and OFF labels
To change the labels of the switchbutton set the desired value in the onLabel
and offLabel
properties.