Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取文本长度,单位为*ngFor_Javascript_Angular_Typescript_Angular Reactive Forms_Angular8 - Fatal编程技术网

Javascript 获取文本长度,单位为*ngFor

Javascript 获取文本长度,单位为*ngFor,javascript,angular,typescript,angular-reactive-forms,angular8,Javascript,Angular,Typescript,Angular Reactive Forms,Angular8,我对Angular(8)相当陌生,我尝试获取输入值的长度,我在*ngFor中创建了输入值,如下所示: <div *ngFor="let panel of panels; index as i" class="panel" [id]="'panel-' + panel.id"> <div>{{ panel.title }}</div> <input matInput placeholder="Bezeichnung" [(ngModel)]

我对Angular(8)相当陌生,我尝试获取输入值的长度,我在
*ngFor
中创建了输入值,如下所示:

<div *ngFor="let panel of panels; index as i" class="panel" [id]="'panel-' + panel.id">
    <div>{{ panel.title }}</div>
    <input matInput placeholder="Bezeichnung" [(ngModel)]="panel.title" />
</div>
我需要什么来获取当前输入的长度

编辑


为了阐明我想要实现的目标:我想要计算当前输入中键入的字符数,并从类(而不是模板本身)触发警报。

.html 试试这个

<div *ngFor="let panel of panels; index as i" class="panel" [id]="'panel-' + panel.id">
    <div>{{ panel.title }}</div>
    <input (change)="onChange({{panel.title}})" matInput placeholder="Bezeichnung" [(ngModel)]="panel.title" />
</div>


onChange(title) {
          console.log(title.length);
         })

{{panel.title}}
onChange(标题){
控制台日志(标题、长度);
})

您的
ngModel
需要绑定到
面板
而不是
ngFor
中的
面板
。您可以使用
索引执行此操作


{{panel.title}}
然后通过传入索引触发组件中的警报,并使用该索引获取面板标题的长度。我在这里使用了
blur
事件

showAlert(索引){
const titleLength=this.panels[index].title.length;
//在此处使用标题的长度调用alert。
}

当前显示输入,用户键入INGA,请稍微澄清一下,获取长度是什么意思?您正试图解决的用例到底是什么?如果你能提供更多的细节,这将有助于提供答案。我想计算用户键入的字符。你在哪里键入??您想在哪里获得结果?\n很抱歉造成混淆,我在减少样本时意外删除了
。现在又回来了虽然这很有效,但我想让它只计算当前输入的字符数,我不知道如何在课堂上访问它?确实如此,非常感谢。我将
(change)
更改为
(ngModelChange)
(不确定这是否真的更好),所以是的,谢谢!我希望你的解释更具描述性。除了添加代码作为响应外,还可以尝试添加一个简短的解释,说明为什么会派上用场。
<input type="text" (input)="inputMthd($event)"  (keyup)="methodCalled($event)">
inputMthd(e){
console.log(e);
event.target.value.length;
}
methodCalled(e){
console.log(e);
e.target.value.length;
}
<div *ngFor="let panel of panels; index as i" class="panel" [id]="'panel-' + panel.id">
    <div>{{ panel.title }}</div>
    <input (change)="onChange({{panel.title}})" matInput placeholder="Bezeichnung" [(ngModel)]="panel.title" />
</div>


onChange(title) {
          console.log(title.length);
         })