javascript - Angular 2 : Unhandled Promise rejection: Cannot assign to a reference or variable

原文 标签 javascript angular ionic2 ngfor

我有一个带有条形码阅读器的 Ionic 2 应用程序,当我遇到这个时,我试图显示数据:

Unhandled Promise rejection: Cannot assign to a reference or variable! ; Zone: ; Task: Promise.then ; Value: Error: Cannot assign to a reference or variable!



我不确定到底出了什么问题,但这是我的模板。我知道当我删除 [[(ngModel)]="val"问题消失了:
<ion-grid padding>
    <ion-col col-8 offset-2>
        <ion-item *ngFor="let val of keys">
            <ion-label stacked>{{ val }}</ion-label>
            <ion-input [(ngModel)]="val" [value]="val"></ion-input>
        </ion-item>
    </ion-col>
</ion-grid>

...这是我的组件和相关功能:
ngOnInit(clientService) {
    this.scanner.scan().then((barcodeData) => {
        this.barcodeData = barcodeData.text;
        this.parseBarcode(this.barcodeData);
    }, 
    (err) => {
        this.clientService.handleResponse(`Ut Oh.. could not initialize barcode reader !: ${err}`)

        // dummy data for testing on laptop when no scanner available
        this.barcodeData = "1159,NAME_HERE,5/16/2017 9:52 AM,111.6,18.6,17.4,19.4,Underfat,92.2,1,59.5,66.4,87.6,4.6,-1,1267,24.8,5.0,15.6,14.8,,25.8,5.2,15.0,14.2,,19.3,1.2,4.2,4.0,,19.0,0.8,4.2,4.0,,11.7,7.0,53.2,50.8,20.0,";

        this.parseBarcode(this.barcodeData);
    });
}

parseBarcode(data) {
    let trimmed_data = this.trimTrailingComma(data);
    let split_data = trimmed_data.split(",");
    let len = split_data.length;

    if ( len == this.cols.length ) {
        this.finalData = this.createDataHash(this.cols, split_data)
        this.keys = Object.keys(this.finalData);
        this.success = true;
    } else {
        console.log(`Off by ${len - this.cols.length}`)
    }
}

trimTrailingComma(str) {
    if ( str[str.length - 1] == "," ) {
        return str.substring(0, str.length - 1);
    }
}

createDataHash(cols, data) {
    let dataHash = {}

    for ( let i in cols ) {
        dataHash[cols[i]] = data[i]
        console.log(dataHash)
    }

    return dataHash
}

以下是列标题,以防出现问题:
this.cols = [
        'someID',
        'Full Name',
        'Date & Time',
        'Weight (lb)',
        'Body Mass Index (BMI)',
        'Body Fat (%)',
        'Fat Mass (lb)',
        'Body Fat Range',
        'Fat Free Mass (lb)',
        'Visceral Fat Rating',
        'Body Water (%)',
        'Body Water (lb)',
        'Muscle Mass (lb)',
        'Bone Mass (lb)',
        'Muscle Score',
        'Basal Metabolic Rate (kcal)',
        'Right Leg Fat (%)',
        'Right Leg Fat Mass (lb)',
        'Right Leg Fat Free Mass (lb)',
        'Right Leg Muscle Mass (lb)',
        'Right Leg Impedance (?)',
        'Left Leg Fat (%)',
        'Left Leg Fat Mass (lb)',
        'Left Leg Fat Free Mass (lb)',
        'Left Leg Muscle Mass (lb)',
        'Left Leg Impedance (?)',
        'Right Arm Fat (%)',
        'Right Arm Fat Mass (lb)',
        'Right Arm Fat Free Mass (lb)',
        'Right Arm Muscle Mass (lb)',
        'Right Arm Impedance (?)',
        'Left Arm Fat (%)',
        'Left Arm Fat Mass (lb)',
        'Left Arm Fat Free Mass (lb)',
        'Left Arm Muscle Mass (lb)',
        'Left Arm Impedance (?)',
        'Trunk Fat (%)',
        'Trunk Fat Mass (lb)',
        'Trunk Fat Free Mass (lb)',
        'Trunk Muscle Mass (lb)',
        'Body Fat Goal (%)',
    ]

最佳答案

ngModel将在您的组件内寻找一个字段进行评估。所以当你在写 [(ngModel)]="val" ngModel会找 this.val在您没有的组件内。

关于javascript - Angular 2 : Unhandled Promise rejection: Cannot assign to a reference or variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44009564/

相关文章:

angular - 组件不是任何 NgModule 的一部分或模块尚未导入到您的模块中

angular - Ionic 2 ionScroll 事件不刷新 View

Ionic2 代理不使用 ionic run 但使用 ionic serve?

javascript - Mongoose 自动增量

javascript - 更改 Angular 模型属性更改的输入类

javascript - mongodb mongoimport从外部文件动态地

Javascript 数组键检索

javascript - 在Angular 5 CLI项目中编译JavaScript文件

angular - 无法在函数 ionic 之外获得 promise 值

javascript - 如何阅读和理解 Angular 模板解析器错误