Nativescript - 单击更新动态文本字段源

Nativescript - Update dynamic textfield source on click

我只想问一下如何在单击按钮时更新动态文本字段? textfield 是动态的,值应该来自 observable。

代码隐藏

var observableModule = require("data/observable");
var source = new observableModule.Observable();

var HomePage = function() {};
HomePage.prototype = new BasePage();
HomePage.prototype.constructor = HomePage;

HomePage.prototype.contentLoaded = function(args) {
    var page = args.object;

    source.textSource = "sample";

    var layout = page.getViewById("stackID");
    var textField = new TextFieldModule.TextField();

    var textFieldBindingOptions = {
        sourceProperty: "textSource",
        targetProperty: "text",
        twoWay: false
    };

   textField.bind(textFieldBindingOptions, source);

   layout.addChild(textField);
}

HomePage.prototype.buttonTap = function() {
  source.textSource = "new word";
  source.update();
}

XML

<stack-layout loaded="contentLoaded" id="stackID">
    <Button tap="buttonTap" text="Update" />
</stack-layout>

我找到了如何通过点击更新源。

HomePage.prototype.onTap = function() {
  source.set("textSource", "new word");
}

来源:http://docs.nativescript.org/cookbook/data/observable