Angular 如何获取typescript文件4中输入字段的值
您可以使用以下代码:Angular 如何获取typescript文件4中输入字段的值,angular,Angular,您可以使用以下代码: hidePara1(item,ii) { this.selectedIndex=-1; this.showHideP = !this.showHideP; //for hiding paragraph this.tbox = !this.tbox; //showing text box this.edt=!this.edt; //hide edt button this.sbtn = !this.sbtn; //display save bu
hidePara1(item,ii)
{
this.selectedIndex=-1;
this.showHideP = !this.showHideP; //for hiding paragraph
this.tbox = !this.tbox; //showing text box
this.edt=!this.edt; //hide edt button
this.sbtn = !this.sbtn; //display save button
}
首先,为了使用
ngModel
<td class="edit-field" *ngIf="selectedIndex==i" >
<input type="text" class="myCl" [(ngModel)]="val" >
</td>
然后可以使用[(ngModel)]
在.ts
文件中获取值
HTML:
import { FormsModule } from '@angular/forms';
您的代码变成:
inputText :string = "I am sample text";
{{i+1}。{{item}}
值
是使用输入字段的此组件的属性。使用[(ngmodel)]
从输入字段中获取值,并通过ng模型方法存储数组的值,然后单击保存按钮传递数组值和参数以保存
它显示了以下错误:无法绑定到“ngmodel”,因为它不是“input”的已知属性。(“it字段”*ngIf=“selectedIndex==i”>][(ngmodel)]=“value[i]”在应用程序模块中注册formsmodule
从'@angular/forms'导入{formsmodule};@NgModule({imports:[BrowserModule,formsmodule]},提供程序:[],引导:[AppComponent],})导出类AppModule{}ngModel
而不是ngModel
让我们来看看。我已经尝试过了,但它不起作用,因为它需要在同一范围内进行onclick,因此hidePara1(item,I)无法使用其值I write console.log(item.value)在hidepara1函数中,它向我显示错误…我还使用console.log(item),它给我提供了未定义的功能。您可以更新问题中的函数代码吗?我已经添加了问题中的函数我已经更新了答案,现在只更改(单击)='hidePara(item,I)
sangwin当我编写console.log(item)时,输入字段是可编辑的在我的para1函数中,它向我显示以前的值而不是新值
<input type="text" [(ngModel)]="inputText">
inputText :string = "I am sample text";
<div id="lists">
<table id='mytable' cellspacing='0' cellpadding='0'>
<tr class="row" [ngClass]="{'selected': i === selectedIndex}" *ngFor= 'let item of list; let i = index' [attr.data-index]='i' [ngStyle]="{'background-color': i%2 == '0' ? 'gray' : ''}">
<td class='default-field'>
<p class='para' *ngIf="selectedIndex!=i ">{{i+1}}. {{item}}</p>
</td>
<td class="edit-field" *ngIf="selectedIndex==i " >
<input type="text" class="myCl" [value]="val" [(ngModel)]="item.inputText">
</td>
<td class='btns'>
<input type='button' value='edit' class='edit-btn' (click)='hidePara(item,i)' *ngIf="selectedIndex!=i">
<input type='button' value='save' class='save-btn' *ngIf="selectedIndex==i" (click)='hidePara1(item,i)'>
<input type='button' value='DELETE' class='delete-btn' (click)='deleterow(i)'>
</td>
</tr>
</table>
</div>