6
 <div id="lists">
            <table id='mytable' cellspacing='0' cellpadding='0'>
                <tr  class="row" [ngClass]="{'selected': i === selectedIndex}"  *ngFor= 'let item of list; let i = index' [attr.data-index]='i'  [ngStyle]="{'background-color': i%2 == '0' ? 'gray' : ''}">
                    <td class='default-field'>
                        <p class='para' *ngIf="selectedIndex!=i  ">{{i+1}}. {{item}}</p>
                    </td>
                    <td class="edit-field" *ngIf="selectedIndex==i   " >
                        <input type="text" class="myCl" [value]="val" >
                    </td>
                    <td class='btns'>

                    <input type='button' value='edit' class='edit-btn'  (click)='hidePara(item,i)' *ngIf="selectedIndex!=i">

                    <input type='button' value='save' class='save-btn' *ngIf="selectedIndex==i" (click)='hidePara1(item,i)'> 
                    <input type='button' value='DELETE' class='delete-btn' (click)='deleterow(i)'>
                    </td>
                </tr>
            </table>

        </div>

Above is my html code, i want to retrieve the value of input field when user click on save button, the hidePara1(item,i) function will execute, i want to get value of input field in hidepara1, how this can be possible.

 hidePara1(item,ii)
 {
  this.selectedIndex=-1;
  this.showHideP = !this.showHideP;  //for hiding paragraph
  this.tbox = !this.tbox;  //showing text box
  this.edt=!this.edt;  //hide edt button
  this.sbtn = !this.sbtn;  //display save button 

 }

3 Answers 3

8

First of all you need to import FormsModule in app.modules.ts in order to use ngModel

import { FormsModule } from '@angular/forms';

Then you can use [(ngModel)] to get the value in .ts file

HTML :

<input type="text" [(ngModel)]="inputText">

TS :

inputText :string = "I am sample text";

Your code becomes :

 <div id="lists">
            <table id='mytable' cellspacing='0' cellpadding='0'>
                <tr  class="row" [ngClass]="{'selected': i === selectedIndex}"  *ngFor= 'let item of list; let i = index' [attr.data-index]='i'  [ngStyle]="{'background-color': i%2 == '0' ? 'gray' : ''}">
                    <td class='default-field'>
                        <p class='para' *ngIf="selectedIndex!=i  ">{{i+1}}. {{item}}</p>
                    </td>
                    <td class="edit-field" *ngIf="selectedIndex==i   " >
                        <input type="text" class="myCl" [value]="val" [(ngModel)]="item.inputText">
                    </td>
                    <td class='btns'>

                    <input type='button' value='edit' class='edit-btn'  (click)='hidePara(item,i)' *ngIf="selectedIndex!=i">

                    <input type='button' value='save' class='save-btn' *ngIf="selectedIndex==i" (click)='hidePara1(item,i)'> 
                    <input type='button' value='DELETE' class='delete-btn' (click)='deleterow(i)'>
                    </td>
                </tr>
            </table>

        </div>
Sign up to request clarification or add additional context in comments.

6 Comments

i write console.log(item.value) in hidepara1 function it show me error.... I also use console.log(item) which give me undefined
Can you update hidePara1() function code in question ?
i have added function in question
I have updated answer, now change only (click)='hidePara(item,i)'
sangwin the input field is editable when i write console.log(item) in my para1 function it shows me previous value not new value
|
0

You can use following code:

 <td class="edit-field" *ngIf="selectedIndex==i" >
      <input type="text" class="myCl" [(ngModel)]="val" >
 </td> 

1 Comment

I tried this already and it's not working, because it require onclick within the same scope, so for this reason hidePara1(item,i) cannot use its value
0

value is the property of this component by using Input field.use [(ngmodel)]

<input type="text" class="myCl" [(ngmodel)]="value[i]" >

get value from input field and store the value of array by ng model method then click the save button pass array valy and parameter to save

<input type='button' value='save' class='save-btn' *ngIf="selectedIndex==i" (click)='hidePara1(value,item,i)'>

7 Comments

It is showing this error: Can't bind to 'ngmodel' since it isn't a known property of 'input'. ("it-field" *ngIf="selectedIndex==i " > <input type="text" class="myCl" [ERROR ->][(ngmodel)]="value[i]" > </td>
register formsmodule in app module import { FormsModule } from '@angular/forms'; @NgModule({ imports: [ BrowserModule, FormsModule]}, providers: [], bootstrap: [AppComponent], }) export class AppModule { }
ngModel instead of ngmodel
|

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.