0

I have created Angular2 code like this to get the data on button click

    <div class="field-panel">
        <div>
          <span>
             <input #newData [(ngModel)]="data.id" type="text" >
             <select #newCategory [(ngModel)]="i.index" class="input-bars">
              <option *ngFor="let data of field" [value]="data.value">{{data.value}}</option>
            </select>
          </span>
        <button class="btn btn-success (click)="addData(newData.value, newCategory.value)">ADD</button>
         </div>
    </div>

    addData(value, dropValue){

        this.Data[index] = value;
    }    

But when the button is clicked the data on the input field is getting lost in ui. How to keep it when the button is clicked and display on the ui as well??

4
  • Could you please post your addData and the essentials of your html that the button is contained in? Commented Nov 3, 2016 at 16:28
  • @silentsod i'm using the select dropdown as well, I have edited the code Commented Nov 3, 2016 at 16:44
  • Honestly, I don't know what you're trying to accomplish with your code, but here's a stab with a plunker, you'll note that clicking add doesn't clear any input fields: plnkr.co/edit/xTBSLfnxDvYXGHM4zjFa?p=preview Commented Nov 3, 2016 at 17:02
  • I know the reason it's becuase you are using push to store the details to the array and i'm using this.Data[index] = value; this to store the data to array Commented Nov 3, 2016 at 17:07

1 Answer 1

1

I'll hazard a guess that you're working in a form, so add type="button" to the button <button class="btn btn-success" (click)="addData(newData.value)">ADD</button>. That should prevent it from thinking the form is submitting and clearing the data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.