Nativescript标签页textfield

时间:2017-10-20 01:43:55

标签: nativescript

我无法从绑定的文本字段中获取值。我使用了标签模板。在Microsoft Android模拟器上。我需要在主标签页面视图模型中添加一些内容吗?

查看

<GridLayout class="page-content">
    <StackLayout orientation="vertical">
        <Label class="page-icon fa" text="&#xf002;"></Label>
        <TextField id="txtSearch" text="{{ search }}" hint="Search" keyboardType="email" autocorrect="false" autocapitalizationType="none" />
        <Button text="Sign in" tap="searchClick" />
    </StackLayout>
</GridLayout>
var SearchViewModel = require("./search-view-model");

var svm = new SearchViewModel();

function onLoaded(args) {
    var component = args.object;
    component.bindingContext = new SearchViewModel();
}

exports.searchClick = function() {    

    svm.searches()
        .catch(function(error) {
            console.log(error);
            dialogsModule.alert({
                message: "Unfortunately we could not find your account.",
                okButtonText: "OK"
            });
            return Promise.reject();
        })
        .then(function() {
            //frameModule.topmost().navigate("views/list/list");
        });
};

exports.onLoaded = onLoaded;

模型 出于某种原因,viewModel.get(&#34; search&#34;)不返回任何内容。按钮发射很好。

const observableModule = require("data/observable");
var fetchModule = require("fetch");

//Info is the returning object
function SearchViewModel(info) {
    info = info || {};

    var viewModel = new observableModule.fromObject({
           search: info.search || ""
    });

    viewModel.searches = function() {
        var test = viewModel.get("search");
        return fetchModule.fetch(config.apiUrl + viewModel.get("search"), {
            method: "GET",
            headers: {
                "Content-Type": "application/json"
            }
        })
        .then(handleErrors)
        .then(function(response) {
            return response.json();
        })
        .then(function(data) {
            config.token = data.Result.access_token;
        });
    };

    return viewModel;
}

1 个答案:

答案 0 :(得分:0)

你可以简单地使用this.search, 或者您可以使用this.get("search")

编辑:也在旁注, 在您的代码隐藏文件中,您正在创建

var svm = new SearchViewModel();

但是你没有将svm指定为bindingContext,而是创建了一个SearchViewModel的新实例。这可能会导致一些意外的行为。