我们正在使用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) 我确信这是我失踪的小事,我会第一个告诉你我不是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,它就会失去参考.我究竟做错了什么?