NativeScript:使用视图模型函数/方法绑定SearchBar提交事件?

时间:2018-03-15 08:44:02

标签: typescript nativescript nativescript-telerik-ui

您好我无法使用我在vew-model.ts中声明的方法绑定NativeScript Searchbar提交事件。

`<SearchBar id="searchBar" hint="Search" text=""  clear="onClear" submit="{{ onSubmit }}"/>`

And the function in viewmodel:

`public onSubmit(args) {
        console.log('pressed');
        let searchBar = <SearchBar>args.object;
        var searchPhrase = searchBar.text;
        this.url = this.url+this.searchPhrase;
        this.getItems(this.url);
        console.log(this.url);
    }`

但这似乎并没有起作用。如果我在代码隐藏文件(main-page.ts)中定义相同的方法,无论我是否将其编写为:

`<SearchBar id="searchBar" hint="Search" text=""  clear="onClear" submit=" onSubmit"/>`

OR

`<SearchBar id="searchBar" hint="Search" text=""  clear="onClear" submit="{{ onSubmit }}"/>`

我是NativeScript的新手,我不知道我是否遗漏了任何东西。我确实检查了this,但它没有帮助,因为那是针对TextFields的。任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

onSubmit应该是ViewModel的属性才能进行事件绑定。更新方法如下。

onSubmit = (args) => {
    console.log('pressed');
    let searchBar = <SearchBar>args.object;
    var searchPhrase = searchBar.text;
    this.url = this.url+this.searchPhrase;
    this.getItems(this.url);
    console.log(this.url);
};