当前位置:网站首页>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';
}
}
}
边栏推荐
- 酒店公共广播背景音乐-基于互联网+的酒店IP网络广播系统设计
- Common interview questions of microservice
- Hotel public broadcasting background music - Design of hotel IP network broadcasting system based on Internet +
- Notepad++ wrap by specified character
- Go practice -- design patterns in golang's singleton
- Detailed explanation of the output end (head) of yolov5 | CSDN creation punch in
- 中职网络子网划分例题解析
- Making coco datasets
- BTC-密码学原理
- 【实战项目】自主web服务器
猜你喜欢

Communication - how to be a good listener?

Ueditor, FCKeditor, kindeditor editor vulnerability

JS scope

Principles of BTC cryptography

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

Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)

今天很多 CTO 都是被幹掉的,因為他沒有成就業務

配置xml文件的dtd

Congratulations to musk and NADELLA on their election as academicians of the American Academy of engineering, and Zhang Hongjiang and Fang daining on their election as foreign academicians

Progressive multi grasp detection using grasp path for rgbd images
随机推荐
獲取並監控遠程服務器日志
Source insight operation manual installation trial
小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
谷歌 | 蛋白序列的深度嵌入和比对
牛客网 JS 分隔符
Obtenir et surveiller les journaux du serveur distant
"250000 a year is just the price of cabbage" has become a thing of the past. The annual salary of AI posts has decreased by 8.9%, and the latest salary report has been released
请求数据库报错:“could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGram
Basic introduction of redis and explanation of eight types and transactions
Redis 入门和数据类型讲解
(subplots用法)matplotlib如何绘制多个子图(轴域)
Gbase8s composite index (I)
Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
乾元通多卡聚合路由器的技术解析
Altaro set grandfather parent child (GFS) archiving
How to set up altaro offsite server for replication
Redis breakdown penetration avalanche
ES7 easy mistakes in index creation
Installing altaro VM backup
How do I migrate my altaro VM backup configuration to another machine?