【问题标题】:how to keep form data from nativescript-localstorage when routing in another component and return back in first component在另一个组件中路由并在第一个组件中返回时如何保留来自 nativescript-localstorage 的表单数据
【发布时间】:2019-06-07 06:18:33
【问题描述】:

在我的项目中你找到here,我想将我的数据保存在存储中,并在表单中显示。家里有数据时,数据总是显示在家里,如果wifi参数local storge为空,则表格为空。如果wifi参数与本地存储中的'Mywifi'值,表单也会显示'Mywifi'

图片Image with data 图片Image with empty input

请问您知道如何在表单中显示数据吗?

【问题讨论】:

标签: typescript local-storage nativescript


【解决方案1】:

您所要做的就是使用FormGroup 方法,.getRawValue() 将表单数据作为对象获取,.patchValue(data) 从对象设置表单值。

您还必须使用routerExtensions.back() 导航回您来自的页面。使用router.navigate(...) 将继续在此处打开新的主页实例。

ngOnInit(): void {
    // Needs relative path to work with Playground
    let LS = require("../nativescript-localstorage");
    // Make sure you don't apply invalid values from your previous cache, you will get invalid boolean exception if then
    this.ConfigurationForm.patchValue(JSON.parse(LS.getItem(ConfigurationComponent.CURRENT_CONFIG) || "{}"));
}

register() {
    // Needs relative path to work with Playground
    let LS = require("../nativescript-localstorage");
    LS.setItem(ConfigurationComponent.CURRENT_CONFIG, JSON.stringify(this.ConfigurationForm.getRawValue()));
    this.routerExtensions.back();
}

Updated Playground

【讨论】:

    猜你喜欢
    • 2019-12-27
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 2022-01-05
    • 2019-11-14
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    相关资源
    最近更新 更多