Ionic 3和Ngzone()不起作用

时间:2018-05-15 11:20:26

标签: angular ionic3 ngzone


我想在蓝牙连接完成后执行一些操作,反之亦然。
处理连接的方案并添加成功和失败处理程序,并在这些处理程序函数中将标志更改为 True False
我使用console.log打印了该值,它在组件文件中更改但不反映在HTML中。
我尝试使用 ngZone ,但它不起作用 成功和失败处理代码如下:

蓝牙服务

import { Injectable } from "@angular/core";
import { BLE } from '@ionic-native/ble';


@Injectable()
export class BlueToothService {

    constructor(private ble: BLE){
    }

     public connect = (deviceId, onConnect, onFailure) => {
        this.ble.isConnected(deviceId)
            .then(response => {
                console.log("BlueToothService :: isConnected Function :: success :: device----", response);
                onConnect(response);
            },
            error =>  {
                this.ble.connect(deviceId)
                    .subscribe(response => {
                        console.log("BlueToothService :: connect Function :: success :: device----", response);
                        onConnect(response);
                    },
                    error =>  {
                        console.log("BlueToothService :: connect Function :: error :: device----", error);   
                        onFailure(error);         
                    });            
        });
    } }

组件文件

import {Component, NgZone} from '@angular/core';
import {Events, IonicPage, NavController, NavParams, ViewController} from 'ionic-angular';

import {BlueToothService} from '../../../providers/bluetooth/bluetooth.service';

@IonicPage()
@Component({
    selector: 'test-data',
    templateUrl: 'test-data.html',
})
export class AddTestKitDataPage {
    public isBluetoothConnected: boolean = false;
    public deviceId: any;

    public connectToBLE() {
        this.blueToothService.connect(this.deviceId, onConnectionSuccess, onConnectionFailure);  //Assume device id is already present
    }

    private onConnectionSuccess = (reason) => {
        this.zone.run(() => {
            this.isBluetoothConnected = true;
            console.log("isBluetoothConnected---", this.isBluetoothConnected);      
        });
    };

    private onConnectionFailure = (reason) => {
        this.zone.run(() => {
            this.isBluetoothConnected = false;
            console.log("isBluetoothConnected---", this.isBluetoothConnected);      
        });
    } }

HTML

<ion-content>

    <div text-center *ngIf="!isBluetoothConnected">
        Bluetooth Connection failure
    </div>

    <div text-center *ngIf="isBluetoothConnected">
        Bluetooth Connection success
    </div>

    <button ion-button full class="primaryBlockButton" (click)="connectToBLE()">Click</button>

</ion-content>

1 个答案:

答案 0 :(得分:3)

由于console.log确实确认在您的情况下数据实际发生了变化,而视图(模板)没有得到更新 - 这暗示没有发生变更检测。

要确认您已经尝试了&#34; hack&#34;根据你的意见,它起作用了:

private onConnectionSuccess = (reason) => { 
    this.zone.run(() => { 
        setTimeout(() => { 
            this.isBluetoothConnected = true; 
            console.log("isBluetoothConnected---", this.isBluetoothConnected); 
        },0) 
     }); 
};

基本上黑客&#34;包装&#34;您的数据将更改为Angular选择的异步(setTimeout)活动。

现在要解决这个问题,你可以确保在你的情况下通过Angular自然拾取的事件发生数据更改(例如添加自定义甚至监听器)。

或尝试在数据更改后手动使用更改检测来检测更改:

导入CDR:

import { ChangeDetectorRef } from '@angular/core';

注入它:

constructor (private cdr: ChangeDetectorRef) {}

将其添加到您的方法中:

private onConnectionSuccess = (reason) => { 
    this.isBluetoothConnected = true; 
    console.log("isBluetoothConnected---", this.isBluetoothConnected);
    this.cdr.detectChanges();
};

尝试这种方法,因为我认为它会比黑客更好。