angular - 在 Angular 5 中动态添加文本框和文本/芯片

标签 angular typescript

我想使用关闭 (X) 按钮动态添加文本框(最多 10 个),然后向其中添加一些文本。单击“保存”按钮后,文本应显示在不同的 View 中。如果单击关闭按钮 (X),添加的文本框或文本应该是可移除的。

View 可以通过编辑\关闭按钮切换。

Add text box and text/chip dynamically in Angular 5

最佳答案

app.component.ts

  fieldArray: Array<any> = [];
  newAttribute: any = {};

  firstField = true;
  firstFieldName = 'First Item name';
  isEditItems: boolean;

  addFieldValue(index) {
    if (this.fieldArray.length <= 2) {
      this.fieldArray.push(this.newAttribute);
      this.newAttribute = {};
    } else {

    }
  }

  deleteFieldValue(index) {
    this.fieldArray.splice(index, 1);
  }

  onEditCloseItems() {
    this.isEditItems = !this.isEditItems;
  }

app.component.html

    <div class="container">
    <br>
    <div class="row">
        <table class="table table-striped table-bordered col-lg-4">
      <caption><i>Add/remove textbox and chip dynamically in Angular 6</i></caption>
            <thead>
                <tr>
                    <th>Item Name
                        <a (click)="onEditCloseItems()" class="text-info float-right">
                            <i class="mdi mdi-{{isEditItems ? 'close' : 'pencil'}} mdi-18px"></i>
                        </a>
                    </th>
                </tr>
            </thead>

            <tbody *ngIf="!isEditItems">
                <tr *ngIf="firstField">
                    <td>
                        <i (click)="firstField = false" class="mdi mdi-close mdi-18px"></i> {{firstFieldName}}
                    </td>
                </tr>
                <tr *ngFor="let field of fieldArray; let i = index">
                    <td *ngIf="field?.name">
                        <i (click)="deleteFieldValue(i)" class="mdi mdi-close mdi-18px"></i> {{field.name}}</td>
                </tr>
            </tbody>

            <tbody *ngIf="isEditItems">
                <tr>
                    <td *ngIf="firstField">
                        <div class="input-group">
                            <div class="input-group-prepend">
                                <div (click)="firstField = false" class="input-group-text"><i class="mdi mdi-close mdi-18px"></i></div>
                            </div>
                            <input [(ngModel)]="firstFieldName" class="form-control py-2 " type="text" name="firstFieldName" placeholder="Item Name">
                        </div>
                    </td>
                </tr>

                <tr *ngFor="let field of fieldArray; let i = index">
                    <td>
                        <div class="input-group">
                            <div class="input-group-prepend">
                                <div (click)="deleteFieldValue(i)" class="input-group-text"><i class="mdi mdi-close mdi-18px"></i></div>
                            </div>
                            <input [(ngModel)]="field.name" class="form-control" type="text" name="{{field.name}}" placeholder="Item Name">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        <button *ngIf="fieldArray.length <= 2" class="btn btn-success btn-sm" type="button" (click)="addFieldValue()" style="margin-right:10px">Add More Item</button>
                        <button (click)="onEditCloseItems()" class="btn btn-primary btn-sm" type="button">Save Items</button>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

Stackblitz demo link 1

已编辑 Stackblitz demo link 2

关于angular - 在 Angular 5 中动态添加文本框和文本/芯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50374655/

相关文章:

Angular @Output 发射仅在第一次有效

typescript - 从以编程方式创建的 AST 打印类型定义

typescript - 确定泛型方法类型的默认参数

html - 使用angular2上的按钮水平滚动

angular - 我如何将 sec 转换为 hh :mm:ss format in angular2

angular - md-form-field 必须包含一个 MdFormFieldControl

angular - 必需的 Input() 参数仍然发出 "has no initializer"错误

angular - 即使 OnPush 策略开启,什么事件也会触发变更检测?

angular - 将 SCSS 与 Angular 2/4 结合使用的架构

javascript - 正确使用 onCompleted 进行 GraphQL 突变