如何使用自动保存一个Nativescript formControlName

问题描述 投票:1回答:2

我有这样的文本字段以html

<TextField class="textfield" row="0" col="1" formControlName="powersupplyid" [text]='myipdevice'> </TextField>

我想自动保存该功能针对当“myipdevice”显示。

getform() {
    ..........
     .....
        this.myipdevice = myipfind;
        console.log('u gjet', this.myipdevice)
        let LS = require("nativescript-localstorage");
        LS.setItem(this.myipdevice);
    }

你可以问我任何想法如何自动保存价值myipdevice whene数据显示在HTML?

谢谢!

angular typescript nativescript autosave
2个回答
0
投票

它已经被保存到你的表单对象,无论你把它叫做..可以说你把它称为“形式”

然后this.form.controls.powersupplyid.value应该有它只要您输入的值自动更新或做的textarea里的任何东西


0
投票

现在,你似乎是做两件事情。一方面,你的TextField结合的反应形式场formControlName,在另一方面,你也结合了[text]财产。我建议只使用formControlName

在你的组件,你可以值,然后推到控制一旦被检索。 (因为你还没有发布完整的代码,我在这里做一些假设。)

因此,在本例中的ngOnInit方法,你可以从存储保存的值。然后用FormGroup更新patchValue推动切换到视图。

此外,还可以通过订阅valueChanges听更改控制。

export class IpDeviceFormComponent implements OnInit {

  form: this.fb.group({
    powersupplyid: [''] // this is the FormControl bound to your TextField
  });

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    // get a previously saved value from somewhere
    getSavedPowerSupplyId().subscribe(id => {
      this.form.patchValue({
        powersupplyid: id
      });
    });

    // listen for any changes to the form
    this.form.valueChanges.subscribe(form => { /* Save new form state */ });
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.