【问题标题】:Unable to fetch a value from multi-select input texbox in angular2无法从角度 2 中的多选输入文本框中获取值
【发布时间】:2018-11-03 17:46:53
【问题描述】:

我创建了一个包含“+”(添加按钮)和“-”(删除按钮)的文本框。当我点击 + 时,我又得到一个 text-box 。当我单击 - 时,我会删除该文本框及其值。为了获取文本框的值,我使用了 ngModel。我没有使用表格就试过了。我面临的问题是 -

[1.]

当我点击“+”按钮时,我成功获得了文本框,并且我也获得了它的值。但是对于使用“+”按钮完成的后续添加,我得到相同的值。当我更改其值时,使用“+”按钮完成的每个其他文本框的值都会更改。

[2.] 我不知道如何从数组变量中删除文本框的值。

请帮忙

代码 - app.component.html

<div *ngIf="addContainer">
  <p style="margin-left: 200px; font-size:18px">Please enter the API 
   Object -</p>

<table align="center">

    <tbody>

      <tr >

          <td  >
            <input  type="text" placeholder="Enter a Node" [(ngModel)]= "firstValue">
          </td>
          <td  >
            <button type="button" style="margin-left: 10px" (click)="addOne(firstValue)" class="btn btn-success"> + </button>
          </td>
          <td>
            <button type="button" style="margin-left: 10px" (click)="deleteOneMore()" class="btn btn-danger"> - </button>
          </td>

      </tr>


      <tr *ngFor="let container of containers; let i = index;" #myElement>
          <ng-container >
            <td  id="1myElement">
              <input *ngIf="addMore" type="text" placeholder="Enter a Node" [(ngModel)]= "addedValue">
            </td>
            <td  id="1myElement">
              <button type="button" style="margin-left: 10px" (click)="addOneMore($event)" class="btn btn-success"> + </button>
            </td>
            <td>
              <button type="button" style="margin-left: 10px" (click)="deleteOneMore()" class="btn btn-danger"> - </button>
            </td>
          </ng-container>
        </tr>

      <tr>

        <td style="text-align:center">
          <button type="button" (click)="showGraphs(firstValue,addedValue)" class="btn btn-dark">Search</button>
        </td>
      </tr>
    </tbody>
   </table>
  </div>

app.component.ts

addOne(firstDropdValue) {

        console.log("inside addOne = firstDropdValue = ", 
      firstDropdValue);

        this.addMore = true;
        this.containers.push(this.containers.length);

    }

  addOneMore(addedValue)
    {
        this.moreValues.push(addedValue);

        console.log("Inside AddOneMore More Values = ", 
      this.moreValues);

    }
    deleteOneMore(){

        this.containers.splice(this.index, 1);
    }


    showGraphs(firstV, addedV) {
        console.log("inside showGraphs()");
        console.log("firstValue =", firstV, "addedValue = ", addedV);
        this.showEwayBill = true;
        this.showCollection = true;
        this.EwayBill();
        this.Collection();
    }

图片-

【问题讨论】:

    标签: javascript html css angular typescript


    【解决方案1】:

    您应该为新添加的值维护一个数组,如下所示,您的 addremove 应该如下所示

    values = ["sometext"];
    
    addOneMore() {
      this.values.push("sivakumar");
    }
    
    deleteOneMore(index) {
      this.values.splice(index, 1);
    }
    

    在模板文件中,您可以使用 valuescontainers 数组进行如下循环

    <table>
      <tr>
        <td>
          <input type="" [(ngModel)]='values[0]'>
        </td>
        <td>
          <button type="button" style="margin-left: 10px" (click)="addOneMore(firstValue)" class="btn btn-success"> + </button>
        </td>
        <td>
          <button type="button" style="margin-left: 10px" (click)="deleteOneMore(i)" class="btn btn-danger"> - </button>
        </td>
      </tr>
    
      <ng-container *ngFor="let value of values; let i = index;">
        <tr *ngIf="i > 0">
          <td id="1myElement">
            <input type="text" placeholder="Enter a Node" [(ngModel)]="values[i]">
          </td>
          <td id="1myElement">
            <button type="button" style="margin-left: 10px" (click)="addOneMore()" class="btn btn-success"> + </button>
          </td>
          <td>
            <button type="button" style="margin-left: 10px" (click)="deleteOneMore(i)" class="btn btn-danger"> - </button>
          </td>
        </tr>
      </ng-container>
    </table>
    

    但是,在这个解决方案中,仍然存在一个问题,在文本框中,我们允许一次输入一个单词,我无法弄清楚原因,如果有人可以,欢迎您编辑答案;-)

    工作堆栈闪电是here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多