代码之家  ›  专栏  ›  技术社区  ›  Mathews Musukuma

如何在离子3的代码中解决这个ngFor

  •  0
  • Mathews Musukuma  · 技术社区  · 7 年前

    我有一个科尔多瓦蓝牙插件,这是工作正常,但当我试图显示配对设备的列表,我无法显示在html页面上,但只是提醒他们。我已经尽了我所能,但我没有在应用程序的html页面上显示数据。我真的不能通过控制台得到错误,因为我是在真正的设备上运行的应用程序。我是新来的离子和角度,任何帮助我会非常感谢。谢谢你

    <ion-header>
      <ion-navbar>
        <ion-title>
          BlueTooth Printer
        </ion-title>
      </ion-navbar>
    </ion-header>
    <ion-content>
     
      <ion-list>
          <ion-item *ngFor=" let device of devicelists">
            {{device?.address}}
          </ion-item>
      </ion-list>
    
    </ion-content>

    这是我的 主页.ts

    import { Component } from '@angular/core';
    import { NavController } from 'ionic-angular';
    
     declare var bluetoothSerial: any;
    
    @Component({
      selector: 'page-home',
      templateUrl: 'home.html'
    })
    export class HomePage {
    
      devicelists: any;
    
      constructor(public navCtrl: NavController) {
       
      }
    
      ionViewDidLoad(){
       this.isEnabled();
       this.listDevices();
      }
    
      listDevices(){
        bluetoothSerial.list(
          function(devices) {
          devices.forEach(function(device) {
            alert(device.address); // This is working just fine
            this.devicelists = device; // But this is what is not working
          })
      }, 
      function(failure){
        alert(failure);
      });
      }
      isEnabled(){
        bluetoothSerial.enable(
          function() {
              alert("Bluetooth is enabled");
          },
          function() {
              alert("The user did *not* enable Bluetooth");
          }
      );
      }
    
      connect(){
        bluetoothSerial.connect(
         function(data){
          console.log("Success");
          alert(data)
        });
      }
      desconnect(){
        bluetoothSerial.disconnect(function(data){
          console.log("Success");
          alert(data)
        },function(err){
          console.log("Error");
          alert(err)
        })
      }
      printText(){
        bluetoothSerial.write(function(data){
          console.log("Success");
          alert(data)
      },function(err){
          console.log("Error");
          alert(err)
      }, "String to Print")
      }
    
    }
    2 回复  |  直到 7 年前
        1
  •  1
  •   SiddAjmera    7 年前

    更改您的实现 listDevices()

    listDevices() {
      bluetoothSerial.list(devices => this.devicelists = devices)
    }
    

    这样你就可以绑定到正确的 this

        2
  •  0
  •   Mathews Musukuma    7 年前

    尝试将回调更改为箭头函数,如下所示

    listDevices(){
    bluetoothSerial.list(
      function(devices) {
      devices.forEach(function(device) {
        alert(device.address); // This is working just fine
        this.devicelists = device; // But this is what is not working
      })
    }
    

      listDevices(){
        bluetoothSerial.list(
          (devices) => {
          devices.forEach((device) => {
            alert(device.address); // This is working just fine
            this.devicelists = device;
          })
      })
    }