SearchBar提交

时间:2016-05-17 09:06:51

标签: javascript mobile typescript nativescript

我正试图获取搜索栏的事件,但没有答案...... 有必要做更多的事情吗?

TS:

import {CompaniesViewModel} from "../../shared/models/companyModel";
import {Page} from "ui/page";
import {SearchBar} from "ui/search-bar";

export function pageLoaded(args) {
    console.log('pageLoaded');
    var page = <Page>args.object;
    page.bindingContext = new CompaniesViewModel();

    var searchBar = new SearchBar();
    searchBar.on(SearchBar.submitEvent, function (args) { 
        console.log("Search for " + (<SearchBar>args.object).text);
    });
    searchBar.on(SearchBar.clearEvent, function (args) {
        console.log("Clear");
    });
}

XMS:

<SearchBar row="1" text="{{ search }}" hint="NIF ou Nome da empresa" id="search" />

1 个答案:

答案 0 :(得分:2)

这是因为searchBar变量使用新的SearchBar初始化,与XML中定义的搜索栏无关。所以你的TS应该改为:

import {CompaniesViewModel} from "../../shared/models/companyModel";
import {Page} from "ui/page";
import {SearchBar} from "ui/search-bar";

export function pageLoaded(args) {
    console.log('pageLoaded');
    var page = <Page>args.object;
    page.bindingContext = new CompaniesViewModel();

    var searchBar = page.getViewById<SearchBar>("search");
    searchBar.on(SearchBar.submitEvent, function (args) { 
        console.log("Search for " + (<SearchBar>args.object).text);
    });
    searchBar.on(SearchBar.clearEvent, function (args) {
        console.log("Clear");
    });
}