Declaring button module

Button module is required to use any button feature.

import * as buttonModule from "tns-core-modules/ui/button";

Other frequently used modules when working with buttons include:

import * as bindable from "tns-core-modules/ui/core/bindable";
import * as observable from "tns-core-modules/data/observable";

Attaching event handler for the button tap event.

  <Button tap="buttonTap" />

Creating a button

var button = new buttonModule.Button();

Setting the text of a button

button.text = "Hello, world!";

Responding to the tap event

button.on(buttonModule.Button.tapEvent, function (args: observable.EventData) {
    // Do something

Binding text property directly to model

var model = new observable.Observable();
model.set("buttonTitle", "OK");
var options: bindable.BindingOptions = {
    sourceProperty: "buttonTitle",
    targetProperty: "text"
button.bind(options, model);
// button.text is now "OK"
model.set("buttonTitle", "Cancel");
// button.text is now "Cancel"

Is this article helpful? Yes / No
Thank you for your feedback!

Tell us how we can improve this article

NativeScript is licensed under the Apache 2.0 license .
© Progress Software Corporation. All Rights Reserved.