Nativescript Switch不会触发初始绑定

时间:2016-12-01 15:33:22

标签: nativescript

我的观点是

<Switch checked="{{ active }}" propertyChange="onCheckChange"/>

exports.onCheckChange = function(args)
{
   //Api Service call
}

实际上我通过API调用绑定了活动值,问题是onCheckChange在初始绑定期间执行了false值,所以每当我最初设置active==true时,api服务电话和加载页面,onCheckChangechecked==false一起执行,任何人都可以给我一个关于此的想法。

注意:Nativescript中的初学者

2 个答案:

答案 0 :(得分:0)

我与已检查的属性进行了大量的斗争,因此我选择了双向绑定,其行为符合预期:

// app.module.ts
import { NativeScriptFormsModule } from "nativescript-angular/forms";
......
@NgModule({
  imports: [
    NativeScriptFormsModule,
    ......
  ],
  exports: [
    NativeScriptFormsModule,
    ......
  ],
  ......

请注意,要使双向绑定正常工作,您需要在app.module.ts或适用模块中导入 NativeScriptFormsModule ,例如:

{{1}}

答案 1 :(得分:0)

two-way data-binding(由leoncc描述)可能特定于 Angular NativeScript。

这是一种没有双向数据绑定的解决方法,希望如果需要,可以更轻松地移植到 plain NativeScript。

在控制器中,我们可以通过Switch查询获取ViewChild的状态:

  checked = true;
  @ViewChild ('switch') private switch: ElementRef;
  switched() {
    let switch: Switch = this.switch.nativeElement;
    this.checked = switch.checked}

在模板中,我们应该调用switched更改处理程序:

<Switch #switch [checked]="checked" (checkedChange)="switched()" class="switch"></Switch>
相关问题