How to get the value of a Textview?


#1

Hi, I have a TextView where the user can type a message.

I need to get the value of the Textview while the user is typing.

I didn’t found clear docs about a simple thing like this.

Somebody have an idea about it?

Thanks.

// HTML: 
<Textview textWrap="true" (textChanged)="onChange($event);"></Textview>

//Typescript:
  private onChange(event: any): void {
        console.log(event);
        this.inputValue = event.value;
  }

#2

@ParVis please take a look at the two-way Data Binding article in the official NativeScript Angular docs - https://docs.nativescript.org/angular/core-concepts/angular-data-binding


#3

HI @Pete.K .

Do you have any idea on how to initialize the model “this.model”?

Some example on how to generate that reference to the model.

Thanks.


#4

Hey @ParVis As @Pete.K suggested, You have to look at two-way Data Binding. This is what you want exactly.

This is how two-way Data Binding works :slight_smile: -

<Textview textWrap="true" [(ngModel)]="chat.text"></Textview>

<Label class="m-15 h2 text-left" text="This is how two-way Data Binding works" [(ngModel)]="chat.text" textWrap="true"></Label>

#5

@ParVis, if you want to track for example the length of the text in the text view white the user is typing you can do it with ngModelChange

<TextView name="myTextView" [(ngModel)]="myTextView" (ngModelChange)="onMyTextViewChange($event)" text=""></TextView>

#6

Thanks to all. Finally I resolved it on this way:

HTML:

<Textview textWrap="true" (textChange)="onTextChange($event)" maxLength="{{maxChars}}"  hint="Your message here." returnKeyType="send"></Textview>

.TS

onTextChange(args) {

	this.message = args.object["text"];

	this.charsRemaining = this.maxChars - args.object["text"].length;

}