警告不会出现在Ionic 2中的(点击)或(点击)事件中

时间:2018-03-04 16:36:14

标签: angular typescript ionic-framework ionic2 ionic3

警告不会出现在离子2中的(单击)或(点击)事件:当我尝试单击离子中的列表项并通过该函数时,该函数不起作用,因为我使用的是本机组件,我只能在不返回运行时错误的设备上进行测试。

我不确定问题是什么,所以任何和所有的帮助将不胜感激。 这是我的代码

import { Component } from '@angular/core';
import { Hotspot, HotspotNetwork } from '@ionic-native/hotspot';
import { IonicPage, NavController, NavParams, AlertController } from 
'ionic-angular';

@IonicPage()
@Component({
   selector: 'page-wifilist',
   templateUrl: 'wifilist.html',
})
export class WifilistPage {

  networks: any;
  wifiCredentials: {
  ssid: string,
  password: string
  }
  constructor(public hotspot: Hotspot, public navCtrl: NavController, 
public alertCtrl: AlertController, public navParams: NavParams) {
  }

  ionViewDidLoad() {
    this.hotspot.scanWifi().then((networks: Array<HotspotNetwork>) => {
      this.networks = networks;
    });
    console.log('ionViewDidLoad WifilistPage');
  }

  setWifiCredentials(ssid: string) { 
    console.log('working');
    this.wifiCredentials.ssid = ssid;
    let passwordAlert = this.alertCtrl.create({
      title: 'Wifi Credentials',
      subTitle: 'Enter password for ' + this.wifiCredentials.ssid,
      inputs: [
        {
          name: 'password',
          placeholder: 'password',
          type: 'password'
        }
      ],
      buttons: [
        {
          text: 'cancel',
          role: 'cancel'
        },
        {
          text: 'connect',
          handler: data => {
            this.wifiCredentials.password = data.password;
          }
        }
      ]
    });
    passwordAlert.present();


    this.alertCtrl.create({
      title: this.wifiCredentials.ssid + ' ' + this.wifiCredentials.password,
      buttons: ['ok']
    }).present();
  }

}

这是html的代码:

<ion-header>
    <ion-navbar>
       <ion-title>
          Wifi Scanner
      </ion-title>
  </ion-navbar>
</ion-header>

<ion-content>
<ion-list>
 <ion-item *ngFor="let x of networks" (tap)='setWifiCredentials(x.SSID)'>
     <ion-avatar item-start>
     <img src="./assets/imgs/wifi-logo.png">
     </ion-avatar >
      <h1 (tap)='setWifiCredentials(x.SSID)'>{{x.SSID}}</h1>
   </ion-item>
</ion-list>
</ion-content>

2 个答案:

答案 0 :(得分:0)

您使用的是单点' ',它指的是字符串类型。您需要使用双引号" ",它引用变量或方法的名称。

 <ion-item *ngFor="let x of networks" (click)="setWifiCredentials(x.SSID)">

答案 1 :(得分:0)

我找到了解决方案,在我的.ts文件中,我的wifiCredentials对象在方法之前尚未初始化。