javascript - 在 Angular/Typescript 中单击按钮/图标时如何将文本字段设置为输入字段?

标签 javascript node.js angular typescript

我是 Angular 6 的新手,我想在单击按钮/图标时将文本字段更改为输入字段。下面提供了部分代码和预期输出。提前致谢。

<div>
   <mat-form-field appearance ="outline">
       <mat-label>Given Name</mat-label>
          <input matInput placeholder="Given name" name="givenName" formControlName="givenName">
       <mat-icon matSuffix">create</mat-icon> #create - pencil icon
   </mat-form-field>
</div>

enter image description here

单击编辑按钮时,它应该显示类似于下图的输入字段。

enter image description here更改内容后(例如:纽约到波士顿),它应该显示类似于下一张图片的文本字段。

enter image description here

最佳答案

您可以使用 *ngIf显示或隐藏编辑/提交按钮

HTML代码:

<h3>Edit form</h3>
<div>
    <mat-form-field class="example-full-width" *ngIf="isEditEnable">
        <input matInput placeholder="Enter name" [(ngModel)]="name">
  </mat-form-field>
  <span *ngIf="!isEditEnable">Name : {{name}}</span>
</div>
<div class="example-button-row">
  <button mat-raised-button color="primary" *ngIf="!isEditEnable" (click)="onEdit()">Edit</button>
  <button *ngIf="isEditEnable" mat-raised-button color="primary" (click)="onEdit()">Submit</button> 
</div>

在 TS 代码中:

isEditEnable : boolean = true; // to show and hide the edit button

name : any;

onEdit(){
    this.isEditEnable =!this.isEditEnable;
}

WORKING DEMO

关于javascript - 在 Angular/Typescript 中单击按钮/图标时如何将文本字段设置为输入字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53879566/

相关文章:

javascript - 如何在 for 循环中使用 setInterval 函数

javascript - Leaflet.js/Browser 由于缺少图 block 文件而返回 500 个内部服务器错误

javascript - nightmare run function queue in while loop(让循环等待队列完成)

node.js - 使用 Kafka 和 NodeJS 的实时通知

Angular Web 组件(元素)输入在 ngOnInit 中未定义

html - 使用 Angular2 将 HTML 页面存储在内存中

javascript - 在网格中绘制单元格

新页面上的 Javascript 加载以将 anchor 移动到顶部固定元素下方

node.js - 蒙戈错误: Can't canonicalize query: BadValue >1

angular-cli "ng build"没有产生一个工作项目?