没有ngModel&的OnBlur事件文本域闪烁

时间:2016-09-19 16:55:31

标签: nativescript angular2-nativescript

Nativescript app:我正在创建dinamy TextFields。

1)Probme - 当我点击dinamicly生成的文本字段时,键盘显示毫秒和消失。当我快速点击几次然后键盘停留。

2)如何在dinamicly生成的TextField上进行onChange / onBlur事件?就像当我更新textField时,我需要调用一个方法。

这是当前列表: (模糊)不起作用: <StackLayout col="1" row="0"> <ListView [items]="categoryService.attributes | async"> <template let-item="item" let-i="index"> <GridLayout rows="50 100"> <Label [text]="item.name"></Label> <TextField #input *ngIf="item.type=='text'" row="1" hint="Enter Value here" [text]="item.name" (blur)="categoryService.onAttributeChange(item, item.type, null, input.value)"></ TextField> <Switch #switch *ngIf="item.type=='checkbox'" row="1" checked="false" (checkedChange)="categoryService.onAttributeChange(item, item.type, null, switch.checked)"></Switch> <DropDown #aa *ngIf="item.type=='select'" row="1" [items]="categoryService.showAttributeValues(item.value)" [selectedIndex]="selectedIndex" (selectedIndexChange)="categoryService.onAttributeChange(item, item.type, aa.selectedIndex)"></DropDown> </GridLayout> </template> </ListView> </StackLayout>

谢谢!

1 个答案:

答案 0 :(得分:0)

关于您的第二个问题,您可以使用textChange方法并返回$event作为参数,这将有助于您逐个获取每个TextField的文字。您可以查看下面的示例代码。关于显示键盘的问题,它可能与listview itemTap事件有关。但是,此问题仅在Android上重现,仍在寻找可能的解决方案。

app.component.html

<StackLayout>
    <ListView [items]="myItems" (itemTap)="onItemTap($event)">
        <template let-item="item" let-i="index" let-odd="odd" let-even="even">
            <StackLayout [class.odd]="odd" [class.even]="even">
                <Label [text]='"index: " + i'></Label>
                <Label [text]='"[" + item.id +"] " + item.name'></Label>
                <TextField (tap)="onTap($event)" hint="Enter text" text="" (textChange)="ontextChange($event)"></TextField>

            </StackLayout>
        </template>
    </ListView>
</StackLayout>

app.component.ts

import {Component, Input, ChangeDetectionStrategy} from '@angular/core';
import {TextField} from "ui/text-field";
import app = require("application");

class DataItem {
    constructor(public id: number, public name: string) { }
}

@Component({
    selector: "my-app",
    templateUrl: "app.component.html",
})
export class AppComponent {
   public myItems: Array<DataItem>;
    private counter: number;
    public status =true;

    constructor() {
        this.myItems = [];
        this.counter = 0;
        for (var i = 0; i < 50; i++) {
            this.myItems.push(new DataItem(i, "data item " + i));
            this.counter = i;
        }
    }

    public onItemTap(args) {
        console.log("------------------------ ItemTapped: " + args.index);
    }

    public ontextChange(args){
        console.log("text "+args.object.text);
    }
}

我希望这会有所帮助