【问题标题】:textarea does not work in angular6textarea 在 angular6 中不起作用
【发布时间】:2018-07-08 17:02:03
【问题描述】:

我正在使用 Angular 6,并尝试将文本区域放入表单中。

这是我目前的代码

<textarea rows="20" cols="50" id="mailtext" required [(ngModel)]="mailtext" name="mailtext" #mailtextvalidityMsg="ngModel" >Hi there</textarea>

我可以看到 html 中的文本区域,但没有呈现“Hi there”文本。我的控制台没有出现任何错误。

如果我删除 [(ngModel)]="mailtext" name="mailtext" #mailtextvalidityMsg="ngModel" 它可以工作。

这仅发生在文本区域。对于同一表单中的其他字段,例如input type="email",则没有问题。

我错过了什么?

谢谢

编辑

我忘了提到我想在文本区域中有换行符和链接,比如

Hi there , 

this is the code you have to use 

Click here

我想要换行,Click here 必须是链接

如果我这样做了

mailtext:any;
this.mailtext = 'Hi there,'+<br>+'this is the code you have to use';

我收到Hi there,NaN

谢谢

【问题讨论】:

  • 它可能在运行时被 ngModel 绑定所取代。如果你想有一个默认值,只需在组件中设置this.mailtext = 'Hi there'
  • @user184994 很棒的收获。把它变成一个常规的答案,这样我就可以把它设置为接受了

标签: angular forms textarea angular6


【解决方案1】:

它可能在运行时被 ngModel 绑定所取代。

如果你想有一个默认值,只需在组件中设置this.mailtext = 'Hi there'

【讨论】:

  • 我忘了提到我想在文本区域内有换行符和链接。如何在组件中设置值时执行此操作?再次感谢
  • @slevin 换行符应与字符串中的\n 一起使用,因此A\nB 将在新行上显示B。另一方面,我认为在文本区域内不可能提供链接
猜你喜欢
  • 2019-02-20
  • 1970-01-01
  • 2017-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-23
  • 1970-01-01
相关资源
最近更新 更多