【问题标题】:How to get the value of the <div> element inside angular reactive form?如何在角度反应形式中获取 <div> 元素的值?
【发布时间】:2019-08-23 17:01:07
【问题描述】:

我正在使用angular reactive 表单,该表单在 div 中包含文本。如何使用响应式表单控件名称在我的组件中获取 div 的值不起作用,因为它可以与 &lt;input&gt;&lt;select&gt; 标记一起使用

已尝试使用包含formcontrolname的代码

HTML代码是


<form [formGroup] ="groupName">
  <div formControlName = "preface">{{text}}</div>
</form>
.ts
this.groupName = this.formBuilder.group({
    preface: [null]
)}

我正在尝试使用以下代码获取组件中的值

let text = this.groupName.get("preface").value; 

我希望 div 元素内的输出在 text 变量帮助内!

【问题讨论】:

  • 你为什么要这样做?
  • @Chellappan ...如果我想编辑表单的场景。那么我应该有权选择要在编辑之前显示的
    元素的前一个值。所以,可能我需要从表单中获取
    元素的文本。我将如何做到这一点?
  • 你有stackblitz吗?
  • 如果在 div 上放置 formControl 指令会抛出错误。如果你想使用选择检查这个:angular.io/api/forms/SelectControlValueAccessor
  • @ng-suhas...否:|

标签: javascript html angular angular-reactive-forms


【解决方案1】:

试试这个,

<form [formGroup] ="groupName">
<div formGroupName="preface">{{text}}</div>
</form>

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签