我有这个page.html:
<form #form="ngForm" (ngSubmit)="submit(form.value)" class="list">
<ion-list>
<ion-item>
<ion-label fixed>SSID</ion-label>
<ion-input name="ssid" type="text" [(ngModel)]="currentSSID"></ion-input>
</ion-item>
<ion-item>
<ion-label fixed>Password</ion-label>
<ion-input name="passwd" ngModel type="password"></ion-input>
</ion-item>
<ion-item>
<ion-label>SSID Hidden?</ion-label>
<ion-select name="notHidden" ngModel>
<ion-option value="NO">NO</ion-option>
<ion-option value="YES">YES</ion-option>
</ion-select>
</ion-item>
<ion-item>
<button ion-button type="submit" block>Add Device
<ion-icon name="md-add"></ion-icon>
</button>
</ion-item>
</ion-list>
</form>
在我的ts文件中,我有这个:
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { LoadingController } from 'ionic-angular';
declare var WifiWizard;
@Component({
selector: 'page-add-device',
templateUrl: 'add-device.html'
})
export class AddDevicePage {
value: any;
currentSSID: string;
constructor(public navCtrl: NavController, public navParams: NavParams , public loadingCtrl: LoadingController, public platform:Platform) {
WifiWizard.getCurrentSSID(this.ssidHandler, this.fail);
}
ssidHandler(s) {
alert(s)
this.currentSSID=s;
}
fail(e){
alert("Please connect to your local WIFI ");
}
}
我真的无法弄清楚为什么this.currentSSID = s将ssid值放在输入字段中... 如果我去放置它:this.currentSSID =&#39;测试123&#39;在构造函数的开头,它绑定到ssid输入字段。
答案 0 :(得分:0)
你能这样试试吗?
您可以将它们创建为本地函数并尝试相同的方法,而不是将处理函数声明为类的一部分: 在你的构造函数中:
WifiWizard.getCurrentSSID(ssidHandler, fail);//removed 'this'
function ssidHandler(s) {//declare inside the constructor locally
alert(s)
this.currentSSID=s;
}
function fail(e){//declare inside the constructor locally
alert("Please connect to your local WIFI ");
}
编辑:与这个&#39;的范围有关。
解决方案:var self = this;//in the constructor outside of the callback functions
和ssidHandler(), self.currentSSID = s;