小编Der*_*ley的帖子

Cordova App中的角度2变化检测和区域不同

我们正在使用Cordova和Angular 2构建应用程序.我有以下代码:

    import { Component, OnInit, ChangeDetectorRef, NgZone } from '@angular/core';
    import { Location } from '@angular/common';

    declare var WL : any;

    @Component({
      selector: 'app-store',
      templateUrl: './store.component.html',
      styleUrls: ['./store.component.css']
    })

    export class StoreComponent implements OnInit {
      status: string;
      document: any;

      constructor(private _location: Location,  private changeDetector: ChangeDetectorRef,
        private zone: NgZone) { }

      ngOnInit() {
        var collectionName = 'people';
        this.status = "JSONStore is not yet initialized!";
        if(typeof WL !== "undefined" && typeof WL.JSONStore.get(collectionName) !== "undefined")
                this.status = "JSONStore is initialized!";
        }
      } …
Run Code Online (Sandbox Code Playgroud)

mobile cordova zone.js angular

6
推荐指数
1
解决办法
827
查看次数

无法在Angular 2 Inside Callback中设置类级别变量

我确信这是我失踪的小事,我会第一个告诉你我不是Angular或ES6专家.我有以下代码集:

getScanner(){
    var that = this;
    cordova.plugins.barcodeScanner.scan(
       function (result) {
            console.log("OBJ "+result);

            that.data = result;

            //THIS IS DEFINED
            console.log("That data first is "+that.data);

             alert("We got a barcode\n" +
                     "Result: " + result.text + "\n" +
                     "Format: " + result.format + "\n" +
                     "Cancelled: " + result.cancelled);              
        }, 
        function (error) {
            alert("Scanning failed: " + error);
        }
    );

    //THIS IS UNDEFINED
    console.log("That data is finally "+that.data);

}
Run Code Online (Sandbox Code Playgroud)

正如您在上面看到的那样,当我第一次在回调中设置that.data时,它会很好地打印到控制台.然后,一旦我到达Cordova电话之外的console.log,它就会失去参考.我究竟做错了什么?

ecmascript-6 angular

2
推荐指数
1
解决办法
98
查看次数

标签 统计

angular ×2

cordova ×1

ecmascript-6 ×1

mobile ×1

zone.js ×1