如何检查angular 2中的文本区域是否为空?
我有一个文本区域框,当我们在文本区域中输入文本时,我需要检查数据。我编写了更改方法,但这不起作用 这是代码如何检查angular 2中的文本区域是否为空?,angular,angular2-forms,Angular,Angular2 Forms,我有一个文本区域框,当我们在文本区域中输入文本时,我需要检查数据。我编写了更改方法,但这不起作用 这是代码 <textarea (change)="textAreaEmpty(textValue)" #textValue></textarea> 我还需要检查用户在文本区域中输入了多少行。我无法在anuglar2中找到任何解决方案,我可以使用jquery或javascript获取数据,但我不想使用它。我想在angular 2中使用它,有人能帮我吗 我可以使用jquery或
<textarea (change)="textAreaEmpty(textValue)" #textValue></textarea>
我还需要检查用户在文本区域中输入了多少行。我无法在anuglar2中找到任何解决方案,我可以使用jquery或javascript获取数据,但我不想使用它。我想在angular 2中使用它,有人能帮我吗
我可以使用jquery或javascript获取数据,但我不想使用它。我想在angular 2中使用它,有人能帮我吗
如果你想做得更“有棱角”,可以使用[ngModel]
textValue
在本例中不是值。它是整个输入元素,因此如果您想检查它是否有自己的值,您需要按如下方式更改html:
<textarea (change)="textAreaEmpty(textValue.value)" #textValue></textarea>
验证
新行
,white spaces
或null
if(value.trim().length === 0)
console.log('No input found')
运行要测试的代码段
插入新行
和空格
,但您将无法从输入字段中获得任何焦点输出
document.getElementsByName(“文本”)[0]。addEventListener('change',onChange);
函数onChange(){
if(this.value.trim().length!=0){
console.log('这是您的输出:')
console.log(this.value)
}
}
textAreaEmpty(textValue.value)
?感谢它的工作。但是,当在文本区域中输入某些内容时,change方法不会调用。如果我在文本区域之外单击,则方法正在调用。请使用input
事件而不是change
。所以(input)=“textAreaEmpty(textValue.value)”
谢谢yurzui,输入事件正在运行没有问题,很高兴我能帮上忙:)
textValue: string = '';
textAreaEmpty(){
if (this.textValue != '') {
console.log(this.textValue);
}
}
<textarea (change)="textAreaEmpty(textValue.value)" #textValue></textarea>
if(value.trim().length === 0)
console.log('No input found')