【问题标题】:Retrieve input field set value检索输入字段设置值
【发布时间】:2018-06-21 16:31:32
【问题描述】:

我正在使用 Angular 5。我已将默认 value(在表单加载时)设置为输入字段。但是,在form 提交时,当我尝试检索这个先前分配的默认value 时,它返回一个空字符串。

我该如何解决这个问题?

<input formControlName="age" type="text" id="ageid"  class="form-item" value="{{student.age}}">

这是我尝试检索值的方式:

console.log("result " + JSON.stringify(this.studentform.value.age));

更新

this.studentform = this.fb.group({
  age: ['', Validators.required]
}, { });

【问题讨论】:

    标签: angular


    【解决方案1】:

    尝试使用以下代码:

    console.log("result " + JSON.stringify(this.studentform.controls.age.value));
    

    甚至可以尝试打印this.studentform 对象来查看控件的状态

    【讨论】:

    • 我重新提出这个问题。它不起作用。当我将默认值设置为0 并尝试使用您的方法检索时,我得到一个空字符串。
    • 能否通过更新原始问题来发布组件 ts 中响应式表单的初始化。
    • 我的意思是当你初始化this.studentform和控件age
    • 你试过[(ngModel)] &lt;input formControlName="age" type="text" id="ageid" class="form-item" [(ngModel)]="student.age"&gt;
    猜你喜欢
    • 2011-09-18
    • 2011-11-28
    • 2013-08-25
    • 2011-01-06
    • 2013-04-12
    • 2015-04-05
    • 1970-01-01
    相关资源
    最近更新 更多