'这'在回调函数

时间:2017-01-26 15:05:53

标签: angular ionic2

我使用离子2 RC5。

在下面的扫描功能中,我称之为:

  

this.navCtrl.push(VerifyVehiclePage)

似乎'这个'在该范围内未知:

Cannot read property 'navCtrl' of null

我不明白为什么,我怎么能让它可用,所以我可以使用navController?

import { Component } from '@angular/core';
import { Platform, NavController } from 'ionic-angular';
import { VerifyVehiclePage } from '../verify-vehicle/verify-vehicle'
import { Setup } from '../../providers/setup'
declare var cordova:any;

@Component({
    selector: 'page-scan-vehicle',
    templateUrl: 'scan-vehicle.html'
})
export class ScanVehiclePage {

    constructor(public platform: Platform, public navCtrl: NavController, public setup: Setup) {}

    public scan () {
        // import Entry Model
        let entry = this.setup.getEntry()
        var types = ["PDF417"];
        var options = {
            beep : true,  // Beep on
            noDialog : true, // Skip confirm dialog after scan
            uncertain : false, //Recommended
            quietZone : false, //Recommended
            highRes : false, //Recommended
            inverseScanning: false,
            frontFace : false
        };
        var licenseiOs = "RZNIT6NY-YUY2L44B-JY4C3TC7-LE5LFU2B-JOAF4FO3-L5MTVMWT-IFFYAXQV-3NPQQA4G";
        var licenseAndroid = "Y5AR6RJ4-PPA6ZDJ6-ABLKN4DE-XZEVSOLL-HLBOUBAE-AQCAIBAE-AQCAIBAE-AQCFKMFM";
        var licenseWP8 = "5JKGDHZK-5WN4KMQO-6TZU3KDQ-I4YN67V5-XSN4FFS3-OZFAXHK7-EMETU6XD-EY74TM4T";
        try {
            cordova.plugins.pdf417Scanner.scan(
                function callback(scanningResult) {
                    if (scanningResult.cancelled == true) {}
                        var resultList = scanningResult.resultList;
                        for (var i = 0; i < resultList.length; i++) {
                            var recognizerResult = resultList[i];
                            if (recognizerResult.resultType == "Barcode result") {
                                var disc = recognizerResult.data.split("%")
                                entry.vehicle.licence_number = disc[6]
                                entry.vehicle.register_number = disc[7]
                                entry.vehicle.vehicle_type = disc[8]
                                entry.vehicle.make = disc[9]
                                entry.vehicle.model = disc[10]
                                entry.vehicle.color = disc[11]
                                entry.vehicle.vin_number = disc[12]
                                entry.vehicle.engine_number = disc[13]

                                this.navCtrl.push(VerifyVehiclePage)
                            }
                        }
                    },function errorHandler(err) {alert('Error: ' + err)},types, options, licenseiOs, licenseAndroid, licenseWP8
            );
        } catch (err) {
            entry.vehicle.licence_number = "CY12345"
            entry.vehicle.register_number = "BFG1212F"
            entry.vehicle.vehicle_type = "Pickup"
            entry.vehicle.make = "Land Rover"
            entry.vehicle.model = "Series 3"
            entry.vehicle.color = "White"
            entry.vehicle.vin_number = "36101299292020"
            entry.vehicle.engine_number = "1234334"

            this.navCtrl.push(VerifyVehiclePage)
        }

    }
}

1 个答案:

答案 0 :(得分:4)

变化

function callback(scanningResult) {

(scanningResult) => {

function errorHandler(err) {

(err) => {

另见https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions