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

时间:2016-12-22 00:12:24

标签: android nativescript

我只是想问一下如何在点击按钮时更新动态文本字段?文本字段是动态的,值应来自可观察的。

代码背后

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>

1 个答案:

答案 0 :(得分:2)

我能够找到如何在点击时更新来源。

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

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