Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 917 of the free egghead.io lessons, plus get Angular 2 content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



Integrate a custom NativeScript UI component plugin via npm and registerElement

2:58 Angular 2 lesson by

Plugins are the key to unlocking many rich features of the native platforms. Let’s learn how to integrate the nativescript-checkbox plugin to register a new custom UI component for use in our views.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Plugins are the key to unlocking many rich features of the native platforms. Let’s learn how to integrate the nativescript-checkbox plugin to register a new custom UI component for use in our views.

Avatar
Guru

it did not work from me as is i.e. installing the plugin checkbox using npm install. On android it could not find the plugin. see the link below for workaround if you are using npm version 3 or greater.
https://github.com/NativeScript/nativescript-angular/issues/423
1) create .npmrc file at root of your project and add legacy-bundling=true
2) remove node_modules and platforms directory
3) run npm install again
4) and tns platform add android
5) run again
e.g. ~/project/ng-native (rxJS)*$ tns livesync android --emulator --watch

You can install any number of npm packages into your project. Let's install the NativeScript checkbox plugin. Interestingly, there is no checkbox component from NativeScript. In fact, this is because there is no checkbox component on iOS at all.

In these cases, NativeScript leaves these type of components up to the community. If we look at the NativeScript checkbox plugin from Brad Martin, we'll see exactly how that component will behave on Android and iOS.

This plugin was designed for a Vanilla NativeScript project. However, we can use it inside of a NativeScript for Angular project by importing from NativeScript Angular element registry. That provides a method called "register element."

That will allow us to name the custom element that we want to register. In this case, we're just going to call it checkbox. Then, we pass a function that is going to point to that class. In this case, we want to require our NativeScript checkbox plugin. That provides a class called "checkbox."

Whenever using a custom UI component that may be coming from a plugin, if you're questionable on what the actual class name is, you can find the .ios, and often the .android files in the root.

If you double check their implementation, you can see that they export a class. It is that named class that is going to be available to us as our UI component.

You can see android and iOS both export the same named class. It's exactly that class we are going to reference as our custom check box component.

It's worth noting that you can register these elements really anywhere in your root component, or even in the bootstrap file, as well, once they're registered, once they are available to be used inside of any NativeScript xml view here.

Let's try out our new checkbox component. Let's drop it inside of the dashboard view. We can find an implementation example on the README, and we can actually copy it and paste it as is, but we'll need to change a couple of things.

First of all, there is no name space with NativeScript for angular. We just registered an element called checkbox. We can use that directly, and then, we want to make sure that we close it properly, because again, with NativeScript for angular, all your tags need to be closed properly.

Then, we don't use this binding syntax with Angular. We just want to convert these to just use Angular's binding syntax. Instead of binding the text, we'll just use a label here, and we'll call it custom checkbox, and for fill color, we'll just say blue.

Let's just add our check prop as bully in initialized false. When we run that, we'll see we now we have a checkbox that has a nice animation. On Android, we get the same nice effect.


Featured course:

Create an SVG Icon System

Create an SVG Icon System

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?