当前位置:网站首页>NG Textarea-auto-resize
NG Textarea-auto-resize
2022-07-03 05:29:00 【SeriousLose】
<textarea (ngModelChange)="onChange()" [(ngModel)]="val" (focus)="onChange()" (blur)="onChange()" #text class="autosize" maxlength={
{maxlength}} rows="1" placeholder={
{placeholder}}></textarea>
<!--text1为影子textarea 用于获取scrollHeight,并将值付给主textarea的height 实现auto resize -->
<textarea class="autosize hidden" rows="1" [value]="val" #text1 ></textarea>
:host {
display: flex;
justify-content: flex-start;
.autosize {
vertical-align: top;
padding: 7px 8px;
font-size: 12px;
width: 200px;
border-radius: 3px;
line-height: 14px;
position: relative;
outline: none;
resize: none;
box-sizing: border-box;
height: 32px;
transition: height 0.2s;
}
.hidden {
position: absolute;
left: -100px;
visibility: hidden;
overflow: hidden;
}
}
import { AfterViewInit, Component, ElementRef, EventEmitter, Input, OnInit, Output, Renderer2, ViewChild } from '@angular/core';
@Component({
selector: 'textarea-auto-resize',
templateUrl: './textarea-auto-resize.component.html',
styleUrls: ['./textarea-auto-resize.component.less'],
})
export class TextareaAutoResizeComponent implements OnInit, AfterViewInit {
@Input('max-height') maxHeight = 100; // 最大高度 默认 100
@Input('maxlength') maxlength = 500; // 最大字数 默认500
@Input('placeholder') placeholder: any; // placeholder 提示语
@Input('g') g: any;
@Input('el') el: any;
@Output('valChange') valChange = new EventEmitter();
@ViewChild('text') text: ElementRef;
@ViewChild('text1') text1: ElementRef;
@Input('value') val = '';
constructor(private rd2: Renderer2) {}
ngOnInit() {}
ngAfterViewInit() {
this.reset();
}
onChange() {
this.reset();
setTimeout(() => {
this.valChange.emit({
value: this.val,
g: this.g,
el: this.el,
});
this.reset();
}, 0);
}
/**
* @description: 改变textarea的高度
* @param {type}
* @return:
*/
reset() {
this.text1.nativeElement.style.width = this.text.nativeElement.scrollWidth + 2 + 'px';
if (this.text1.nativeElement.scrollHeight < this.maxHeight) {
this.text1.nativeElement.style.width = this.text.nativeElement.scrollWidth + 19 + 'px';
this.text.nativeElement.style.height = this.text1.nativeElement.scrollHeight + 2 + 'px';
} else {
this.text.nativeElement.style.height = this.maxHeight - 0 + 2 + 'px';
}
}
}
边栏推荐
- Progressive multi grasp detection using grasp path for rgbd images
- C language program ideas and several commonly used filters
- Notepad++ wrap by specified character
- Skip table: principle introduction, advantages and disadvantages of skiplist
- Transferring images using flask
- Simpleitk learning notes
- JS scope
- Introduction to deep learning (II) -- univariate linear regression
- How to install and configure altaro VM backup for VMware vSphere
- Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément
猜你喜欢

6.23星期四库作业

Win10 install pytullet and test

Common interview questions of microservice

How to set up altaro offsite server for replication

Training method of grasping angle in grasping detection

Communication - how to be a good listener?

Gbase8s composite index (I)

@Autowired 导致空指针报错 解决方式

appium1.22.x 版本後的 appium inspector 需單獨安裝

期末复习(Day5)
随机推荐
大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南
"C and pointer" - Chapter 13 function of function pointer 1 - callback function 1
XML Configuration File
牛客网 JS 分隔符
Altaro requirements for starting from backup on Hyper-V
BIO、NIO、AIO区别
Go practice -- gorilla / websocket used by gorilla web Toolkit
The IntelliJ platform completely disables the log4j component
Best practices for setting up altaro VM backups
(subplots用法)matplotlib如何绘制多个子图(轴域)
Differences among bio, NiO and AIO
Skip table: principle introduction, advantages and disadvantages of skiplist
BTC-密码学原理
Why should we rewrite hashcode when we rewrite the equals method?
Bluebridge cup real topic 2020 palindrome date simulation construction provincial competition
Web APIs exclusivity
Introduction to deep learning - definition Introduction (I)
(perfect solution) how to set the position of Matplotlib legend freely
Based on RFC 3986 (unified resource descriptor (URI): general syntax)
Yolov5 input (I) -- mosaic data enhancement | CSDN creative punch in