0

I have a array of items and each item will display a link. If any button is clicked, I would like to replace it with an input text box.

*** Template ***
<ng-container *ngFor="let item of items">
  <p>
    <button id="item-{{ item.id }} " (click)="showInput($event, item.id)"></button>
  </p>
</ng-container>


  *** Component ***
  showInput(ev: MouseEvent, id:number){
    ev.stopPropagation();
    let inputHtml = `<input type=\'text\' id=\'${id}\'/>`;
    console.log(ev.srcElement);
    // Replace element with input
    console.log(id);
  }

3 Answers 3

1

There is no need of function or extra efforts for this , you can simply do it this way :

<ng-container *ngFor="let item of items">
  <p>
    <button *ngIf='!item.inputshow' id="item-{{ item.id }} " (click)="item.inputshow = !item.inputshow"> Button {{ item.id }} </button>
    <input *ngIf='item.inputshow' type='text' id='{{item.id}}' />
  </p>
</ng-container>

WORKING DEMO

*ngIf='!item.inputshow' 
// first it will check for inputshow (First time it will be undefined)
(click)="item.inputshow = !item.inputshow"
// but as soon as user clicks on it it will initialised as boolean true
Sign up to request clarification or add additional context in comments.

5 Comments

Please let me know the reason behind downvotes , downvoters .
I think the downvotes are due to the answer provide by @boris is almost same as yours and the user has written his issue on the comment also. So I think that you should update your answer for the same. Don't you?
@swetanshkumar , Updated thanks , Still that's not the reason being downvoted.
I too faced the issue but still not able to find the solution and this is why I was going through each answer.
@swetanshkumar , please create a demo on stackblitz , and genrate issue , So I can check overthere.
0

Here's one way to do it (I also included a stackblitz: https://stackblitz.com/edit/angular-m47ktp):

<ng-container *ngFor="let item of items">
  <p>
    <button *ngIf="item.show === true; else inputField"
            [id]="'item-' + item.id" 
            (click)="item.show = false">
          {{item.text}}
    </button>
    <ng-template #inputField >
        <input type="text" [id]="item.id" />
    </ng-template>
  </p>
</ng-container>

Sample dataset:

 items: [
    {
      id: 1,
      text: 'Button 1',
      show: true,
    },
    {
      id: 2,
      text: 'Button 2',
      show: true,
    },
    {
      id: 3,
      text: 'Button 3',
      show: true,
    },
    {
      id: 4,
      text: 'Button 4',
      show: true,
    }
  ]

3 Comments

I am trying to not have the input in the DOM until the link is clicked. Actually the input is a datepicker which slows down actions on the page considerably.
When you use *ngIf directive the element is not rendered unless the condition is met. It's not going to be in the dom, it's going to appear as a comment until the condition evaluates to true. So it's not going to slow anything down.
As a general rule html elements belong in the template, so using a line of html code as a string in your model is very rarely a good idea.
0

Easiest Method is as follows.By using only the template you can do this as follows. By using *ngIf it wont be rendered or created in the DOM unless the condition is true.So this is the best way.

<ng-container *ngFor="let item of items">
  <p>
    <button *ngIf="!item.isInputVisible"  (click)="item.isInputVisible=true"> {{ item.id }} </button>
    <input *ngIf="item.isInputVisible" type="text"  />
  </p>
</ng-container>

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.