问题描述
我的观点是
<Switch checked="{{ active }}" propertyChange="onCheckChange"/>
exports.onCheckChange = function(args)
{
//Api Service call
}
实际上,我是通过API
调用绑定活动值的,而问题是onCheckChange
在初始绑定期间以假值执行,因此每当我最初通过api服务调用设置active==true
并加载页面时,onCheckChange
用checked==false
执行,任何人都可以给我一个想法.
Actually I am binding the active value by API
call and the issue is that onCheckChange
gets executed during the initial binding with false value, so whenever I initially set the active==true
by api service call and load the page, the onCheckChange
is executed with checked==false
, can anyone give me an idea about this please.
注意:Nativescript的初学者
推荐答案
双向数据绑定(由leoncc
描述)可能特定于 Angular NativeScript.
The two-way data-binding (described by leoncc
) might be specific to the Angular NativeScript.
这是一种没有双向数据绑定的解决方法,希望如果需要的话,可以更轻松地移植到 plain NativeScript.
Here's a workaround without the two-way data binding, hopefully it will be easier to port to the plain NativeScript if needs be.
在控制器中,我们可以通过ViewChild
查询获取Switch
的状态:
In the controller we can get the state of the Switch
with a ViewChild
query:
checked = true;
@ViewChild ('switch') private switch: ElementRef;
switched() {
let switch: Switch = this.switch.nativeElement;
this.checked = switch.checked}
在模板中,我们应该调用switched
更改处理程序:
And in the template we should invoke the switched
change handler:
<Switch #switch [checked]="checked" (checkedChange)="switched()" class="switch"></Switch>
这篇关于Nativescript开关不触发初始绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!