微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

角度 – 离子3和Ngzone()不起作用

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

BluetoothService

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>

解决方法

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

要验证您是否已经尝试过“hack”,并根据您的评论它的工作原理:

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

基本上,hack将您的数据更改“包装”为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();
};

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐