【发布时间】:2018-01-28 22:38:14
【问题描述】:
Angular 5.2.0 和 TypeScript 2.6.2。
我正在使用following plugin 读取 NFC 标签。监听器在用户按下按钮时启动。
<button (tap)="doStartNdefListener()" text="Get NFC"></button>
<Label [text]="nfcText" textWrap="true"></Label>
问题 - 即使 doStartNdefListener() 中的回调成功扫描标签,将输出保存在 nfcText 中,从内部输出值回调,即使 nfcText 的值已更改,它也不会更新 UI。
import { Component } from "@angular/core";
import { Nfc, NfcTagData, NfcNdefData } from "nativescript-nfc";
export class StartComponent {
public nfc: Nfc;
public nfcText: string;
constructor(){
this.nfc = new Nfc();
}
public doStartNdefListener() {
this.nfc.setOnNdefDiscoveredListener((data: NfcNdefData) => {
if (data.message) {
let tagMessages = [];
data.message.forEach(record => {
tagMessages.push(record.payloadAsString); });
console.log(tagMessages.join(", "));
this.nfcText = tagMessages.join(", ");
console.log(this.nfcText);
}
},
{ stopAfterFirstRead: true, scanHint: "Scan the tag!" })
.then(() => this.nfcText = "Listening for tag")
.catch(err => alert(err));
}
}
两个控制台输出都打印出扫描的 NFC 标签值,但标签没有得到更新。
编辑:
有趣的是,在我运行 doStartNdefListener() 函数后执行另一个函数后 UI 会更新。
【问题讨论】:
-
你可以试试this answer推荐的3种方法。
标签: angular typescript nativescript angular2-nativescript