NativeScript“Switch”组件

时间:2017-12-10 17:57:05

标签: nativescript angular2-nativescript nativescript-telerik-ui

我看到Switch组件出现了一个非常奇怪的行为......我在ng-template中使用它,每次我'切换'一个,其他每个第10个Switch元素都打开。不知道它来自哪里,特别是因为'checked'属性不是动态的(见下文)。有其他人遇到过这个问题吗?此外,即使我看到其他开关打开(或关闭,基于之前的状态),实际的“onInviteContact()”仅触发一次,对于有意点击的实际Switch组件。

<RadListView
        [items]="contacts"
        class="list-group" width="100%">
    <ng-template tkListItemTemplate let-contact="item">
        <GridLayout class="contactItem" columns="2*, 3*, *, *" width="100%" orientation="horizontal">
            <Image col="0" *ngIf="contact.photo" [src]="contact.photo"
                   stretch="aspectFill" width="40%"></Image>
            <Image col="0" *ngIf="!contact.photo" src="res://profile_image"
                   stretch="aspectFill" width="40%"></Image>
            <Label col="1" class="nameLabel" [text]="contact.name"></Label>
            <Switch col="2" class="switchElement" checked="false"
                    (checkedChange)="onInviteContact($event, contact)"></Switch>
        </GridLayout>
    </ng-template>
</RadListView>

1 个答案:

答案 0 :(得分:1)

此行为唯一可能的解释是使用回收本机视图

https://github.com/NativeScript/NativeScript/blob/699e6f5da8ad79f5795e3758365115d681a146c2/tns-core-modules/ui/switch/switch-common.ts#L10

如您所见,它已被设置为“自动”

我怀疑当本地视图在rad列表视图或列表视图中被回收时,你打开的开关的本机视图被回收并被使用。

为避免这种情况,您应该将checked属性绑定到某个observable。

希望这能回答你的问题:)

干杯!