Ionic 3 how to use textarea ngModel and default value?

最后都变了- 提交于 2019-12-10 14:43:49

问题


I am new in ionic and have a problem with textarea. This is my code:

<textarea  [(ngModel)]="userData.aboutme" name="" id="" cols="30" rows="20"  
    value="{{ about_me}}" style="width:100%; padding: 10px; margin-top: 3px;" > 
</textarea>

The problem is that the value is not showing inside textarea. It's show only if i remove the [(ngModel)]. I need help for this thanks a lot


回答1:


You need to use ion-textarea.

Note: This is just an example.Adjust it as you wish.

Working stackblitz

html

 <ion-item>
  <ion-textarea placeholder="Tap here" 
      [(ngModel)]="note" name="note" autocomplete="on" autocorrect="on"></ion-textarea>
 </ion-item>

.ts

  note: string = "My Default Text";
  constructor(public navCtrl: NavController) {

  }

Offical doc about ion-textarea




回答2:


on ion-textarea on .html

 <ion-item>
   <ion-label floating>Content</ion-label>
   <ion-textarea #myInput id="myInput" rows="1" maxLength="500" 
  (keyup)="adjust()" [(ngModel)]="text.content"></ion-textarea>
 </ion-item>

On.ts File,

  import { Directive, HostListener, ElementRef } from '@angular/core';


 @IonicPage()
 @Component({
   selector: 'page-post-text',
   templateUrl: 'post-text.html',
 })

 @Directive({
         selector: 'ion-textarea[autosize]' // Attribute selector,
           })
  export class PostTextPage {

  @HostListener('document:keydown.enter', ['$event']) 
  onKeydownHandler(evt: KeyboardEvent) {
  this.adjust()
  }



   Text = {} as Text;

   constructor(public element:ElementRef) {}

   ngAfterViewInit(){
   this.adjust()
   }

  adjust():void {
  let textArea = 
  this.element.nativeElement.getElementsByTagName('textarea')[0];
  textArea.style.overflow = 'hidden';
  textArea.style.height = 'auto';
  textArea.style.height = (textArea.scrollHeight + 32) + "px";
  }
 }

And you are good to go!



来源:https://stackoverflow.com/questions/47330808/ionic-3-how-to-use-textarea-ngmodel-and-default-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!