Button

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.

<Page>
  <Button tap="buttonTap" />
</Page>

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"

Stay connected with NativeScript

(expect a newsletter every 4-8 weeks)

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