我有文本区域框,当我们在文本区域输入文本时,我需要检查数据。我写的更改方法,但在这里是不起作用的代码。
<textarea (change)="textAreaEmpty(textValue)" #textValue></textarea>
组件
textAreaEmpty(text:string){
if(text.length > 0)
console.log(text);
}
我还需要检查用户在文本区域中输入了多少行。我在anuglar2中找到了任何解决方案,我可以使用jquery或javascript获取数据,但我不想使用它们。我想在angular 2中使用它,有没有人可以帮我?
发布于 2017-03-17 15:46:45
我可以使用jquery或javascript获取数据,但我不想使用它们。我想在angular 2中使用它,有没有人可以帮我?
如果你想做得更“Angularish”,可以使用[ngModel]
。
<textarea [(ngModel)]="textValue" (ngModelChange)="textAreaEmpty()"></textarea>
TS:
textValue: string = '';
textAreaEmpty(){
if (this.textValue != '') {
console.log(this.textValue);
}
}
发布于 2017-03-17 15:47:31
在这种情况下,textValue
不是一个值。它是整个input元素,所以如果你想检查它是否有自己的值,你需要修改你的html,如下所示:
<textarea (change)="textAreaEmpty(textValue.value)" #textValue></textarea>
发布于 2018-07-24 11:23:48
针对white-spaces
new-lines
**,** white-spaces
或new-lines
**,的验证
if(value.trim().length === 0)
console.log('No input found')
测试的运行代码片段
插入new-lines
和white-spaces
,但不会从输入字段中获得任何输出
document.getElementsByName("text")[0].addEventListener('change', onChange);
function onChange(){
if (this.value.trim().length != 0) {
console.log('Here is your output: ')
console.log(this.value)
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<textarea name="text" rows="3" class="form-control" placeholder="Write and check"></textarea>
https://stackoverflow.com/questions/42861671
复制