【问题标题】:How do i populate my Form from my data saved in localstorage如何从我保存在本地存储中的数据中填充我的表单
【发布时间】:2015-07-01 13:31:26
【问题描述】:

我想用我保存在本地存储中的这个值填充我创建的表单。

var vm;
  vm = this;
  vm.data = {};

  var memberData = JSON.parse(localStorage.getItem('memberData'));
  vm.data.mobile =  memberData.mobile;

这是我要填充的表单:

<div class="item item-icon-left"><i class="icon ion-iphone assertive"></i><span  class="label">Mobile</span><span class="item-note">
 <input type="tel" name="mobile" placeholder="Contact" ng-model="profile.data.mobile" required="required" style="background-color:transparent;"/>
  <div class="helpers" ng-if="profileForm.mobile.$touched">
                  <span ng-if="profileForm.mobile.$error.required" class="help-block" style="color:red;">Enter a Phone No.!</span>
                  </div>
                </span>
                </div>

【问题讨论】:

  • 无法理解。请您详细说明或创建一个小提琴?
  • 所以我有一个 Js 文件,它是我的控制器,其中包含我的所有值,现在我已将所有值(移动)保存在本地存储中。然后我想用该数据(移动)填写我的空表单,但我不确定如何,我是 js 和 Angular 框架的新手。谢谢帮助

标签: javascript angularjs html forms local-storage


【解决方案1】:

如果您的表单是示例中的输入标签,这可以通过选择元素并将value 属性设置为您想要的属性来轻松完成,例如(在您的情况下):

  // since your input has no ID, we'll use getElementsByName instead
  // this returns an array of elements, so we'll choose the first one
  // using an ID would be better practice
  var mobileInput = document.getElementsByName('mobile')[0];

  // then we modify the value attribute with your value from above
  mobileInput.value = vm.data.mobile;

W3 Schools 阅读属性和其他相关内容,或查看Mozzila's specs on the input tag 以获取有关如何使用 JavaScript 使用它的提示!

【讨论】:

  • 所以目前它是一个输入类型=tell,我可以将 vm.data.mobile 作为占位符吗?我希望用户看到他们可以更新表单。谢谢
  • jsfiddle.net/not_inept/as22rkoL/1 你是这个意思吗?您可以使用element.attribute = value 访问大多数属性。可通过element.placeholder 访问占位符。
【解决方案2】:

您是否在视图中使用ng-controller="[Controller] as profile"?如果没有,那么

vm.data.mobile =

应该改为

vm.profile.data.mobile =

【讨论】:

  • 我将作为我的控制器的 Js 文件和作为空白表单的 html 分开。感谢您的帮助,我是 Angular js 和 html5 的新手
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-23
  • 1970-01-01
  • 1970-01-01
  • 2021-06-01
  • 2019-10-03
  • 2021-09-21
  • 1970-01-01
相关资源
最近更新 更多