【问题标题】:Need help changing Text of label in TS Code需要帮助更改 TS 代码中的标签文本
【发布时间】:2019-05-30 22:26:46
【问题描述】:

在我的 html 中,我有一个标签,其中显示了我为特定实例提交给服务器的大量图像。我现在的问题是,当我上传图像时,它不会刷新图像计数,我必须关闭移动应用程序并再次打开才能看到图像计数器上升/。

图片上传成功后我尝试更改Typescript代码中的变量,但String没有改变

<StackLayout class="m-10">
   <Label [text]="imagesCount + ' Photos Uploaded'" verticalAlignment="center" class="lbl-info" horizontalAlignment="center" textWrap="true"></Label>
</StackLayout>
get imagesCount() {
    this._imagesCount = workAttachments.length;

    return this._imagesCount;
}

我希望图像标签从 0 Photos Uploaded 变为 1 Photos Uploaded

-- 编辑--

这就是我上传图片的方式

doFileUpload(file: any) {
        let actualFile = fs.File.fromPath(file);
        let base64 = android.util.Base64.encodeToString(actualFile.readSync(), android.util.Base64.NO_WRAP);
        let workOrderAttachment = new WorkOrderAttachment(new Attachment(base64, file.replace(/^.*[\/]/, ''), 0), WorkOrderAttachmentType.PHOTO, '');

        this._service.workOrderAttachment(this.job.id, workOrderAttachment, ['id']).subscribe(result => {
            if (result == null) {
                UserInterfaceUtil.showError("Error Uploading images.", "");
            } else {
                UserInterfaceUtil.showInfo("Photos uploaded successfully.", "");
                this._imagesCount += 1;
            }
        }, error => {
            UserInterfaceUtil.handleError(error);
            console.log(error);
        });
    }

【问题讨论】:

  • 我不确定您是如何更新 workAttachments 数组的。如果更新发生在 NgZone 中,那么您应该看到计数已经更新。我们需要一个可以重现问题的完整示例。

标签: nativescript nativescript-angular


【解决方案1】:

在 TypeScript 中创建 get 属性时,需要 return 一个值。

public get imagesCount(): number {
    this._imagesCount = workAttachements.length;

    return this._imagesCount;
}

查看“访问器”文档:https://www.typescriptlang.org/docs/handbook/classes.html

希望对你有帮助。

【讨论】:

  • 但默认情况下访问器是公开的,你不必明确提及。
  • 确实是@Manoj。我习惯于指定访问器,这就是我指定它的原因。
  • 对不起@Eastrall,看起来问题已被编辑以包含您的修复,当我比较代码时,我认为单独更新访问器并不重要。如果人们在使用答案时投票就好了。
  • 我确实更改了代码,是的,返回值仍然存在。我现在所做的是从 HTML 中调用 getter 并在上传图像时增加 TS 中的变量,但 Label 的文本仍然没有改变。当我点击页面上的一个按钮时,标签就会改变。
  • 您可以在上传完成后刷新变量。我不知道你是如何上传文件的,但如果你使用 observable,你可以订阅它并刷新你的变量。 uploadService.uploadFile(...).subscribe(result =&gt; refreshLabel());
【解决方案2】:

我可以正确完成此操作的唯一方法是在表单上添加一个按钮以刷新标签。这不是做我需要做的最好的方式,但它已经完成了。

【讨论】:

    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多