Home > front end >  Get the value of ngModel without performing any action [Ionic]
Get the value of ngModel without performing any action [Ionic]

Time:05-25

When a user clicks on a button on the same HTML page, I want to send the value of <ion-text> to a TypeScript file. However, it did not work.

  <ion-text [(ngModel)]='xy' ngDefaultControl  >'variables from a global provider' </ion-text>
  <ion-button (click)="callFun()">Save</ion-button>

TypeScript file:

    xy:string;
    ngOnInit() {
     console.log(this.xy);
    }   
    callFun(){
 console.log(this.xy);
    }

In the console, it returns undefined. How can I obtain it? I'm unable to use the (ionChange) function with ion-text.

CodePudding user response:

Sorry, I'm from Angular, but to work [ngModel] the ion-text component should to have a property "value",

I imagine you can use

   <ion-text #myText>{{global.x}}</ion-text>`

   <ion-button (click)="callFun(myText)">Save</ion-button>

   callFun(text:any){
     console.log(text.innerHTML);
    }

But, apologies, I don't know about Ionic :(

CodePudding user response:

why not use ion-input type="text" instead of ion-text?

  • Related