在离子中自动生长文本区域
Auto growing textarea in ionic
我试图添加一个自动增长的文本区域到我的应用程序,但由于某种原因它不工作。我使用的模块是https://github.com/tagged/autogrow(这是离子论坛上推荐的)
上面的答案没有缩小-这里是一个改进的版本:
https://codepen.io/benshope/pen/xOPvpmangular.module('app').directive('expandingTextarea', function () {
return {
restrict: 'A',
controller: function ($scope, $element, $attrs, $timeout) {
$element.css('min-height', '0');
$element.css('resize', 'none');
$element.css('overflow-y', 'hidden');
setHeight(0);
$timeout(setHeightToScrollHeight);
function setHeight(height) {
$element.css('height', height + 'px');
$element.css('max-height', height + 'px');
}
function setHeightToScrollHeight() {
setHeight(0);
var scrollHeight = angular.element($element)[0]
.scrollHeight;
if (scrollHeight !== undefined) {
setHeight(scrollHeight);
}
}
$scope.$watch(function () {
return angular.element($element)[0].value;
}, setHeightToScrollHeight);
}
};
});
这将改变你所有的文本区域的大小。
希望有帮助!
我写了一个非常简单的指令,与Ionic 2和ion-textarea
一起工作。
import { Directive, HostListener, ElementRef } from "@angular/core";
@Directive({
selector: "ion-textarea[autoresize]" // Attribute selector
})
export class Autoresize {
@HostListener("input", ["$event.target"])
onInput(textArea: HTMLTextAreaElement): void {
this.adjust();
}
constructor(public element: ElementRef) {
}
ngOnInit(): void {
this.adjust();
}
adjust(): void {
let ta = this.element.nativeElement.querySelector("textarea");
ta.style.overflow = "hidden";
ta.style.height = "auto";
ta.style.height = ta.scrollHeight + "px";
}
}
要点如下:https://gist.github.com/maxt3r/2485356e91a1969bdb6cf54902e61165
编辑:看看要点,看看其他人的其他建议。
我发现了一个更好的方法来做到这一点,而不使用任何其他第三方库或指令。
$scope.updateEditor = function() {
var element = document.getElementById("page_content");
element.style.height = element.scrollHeight + "px";
};
那么只需在文本区添加ng-keypress="updateEditor()"就可以了。
<textarea ng-keypress="updateEditor()" ng-model="bar"> </textarea>
我希望这对将来可能面临这个问题的其他人有所帮助。
更新:这是一个代码依赖:http://codepen.io/kpourdeilami/pen/KDepk
更新2:使用@benshope
提供的代码片段更新3:如果你使用Ionic/Angular 2,请使用"Max Al Farakh"提供的答案
试试Angular-Elastic。这是一个angular指令,用来自动展开文本区域。使用电源安装
bower install angular-elastic
将其添加到您的项目中,然后您可以将其用作属性
<textarea msd-elastic ng-model="foo"> </textarea>
或作为class
<textarea class="msd-elastic" ng-model="bar"> </textarea>
从Ionic 4.4它是内置的,参见autoGrow
属性:
<ion-textarea auto-grow="true" rows="1"></ion-textarea>
你是说垂直自动生长吗?我试过了:
<textarea ng-model='doc.description'
rows='{{doc.description.length/50 + 1}}'
cols='50'></textarea>
有点粗糙,但是在确定了预期的列长度之后,让我们基于输入文本的长度来定义行长度。当我开始打字时,它开始垂直生长!
对于ionic-5,有一个名为auto-grow的选项,请在视图中将其设置为true。在css中,设置min-height, max-height,来控制文本的生长。
ion-textarea {
min-height: 100px;
max-height: 200px;
}
同样,在上面的修复之后,如果您在占位符文本中得到一些奇怪的行为,请在离子文本区域
中添加以下内容::ng-deep textarea {
min-height: 100px;
}
如果它可以为某人服务,我改变了一点benshope的解决方案,因为我需要文本区域即使在用户执行回车时也会增长。
所以不是监听输入值的变化(当做回车时并不总是触发),我监听textarea上的input
事件。
(function () {
'use strict';
angular
.module('app')
.directive('expandingTextarea', expandingTextarea);
function expandingTextarea() {
return {
restrict: 'A',
controller: function ($scope, $element, $attrs, $timeout) {
$element.css('min-height', '0');
$element.css('resize', 'none');
$element.css('overflow-y', 'hidden');
setHeight(0);
$timeout(setHeightToScrollHeight);
function setHeight(height) {
$element.css('height', height + 'px');
$element.css('max-height', height + 'px');
}
function setHeightToScrollHeight() {
console.log('set height');
setHeight(0);
var scrollHeight = angular.element($element)[0]
.scrollHeight;
if (scrollHeight !== undefined) {
setHeight(scrollHeight);
}
}
angular.element($element)[0].addEventListener("input", setHeightToScrollHeight);
}
};
}})();
just_install:安装角弹性或
npm install angular-elastic;
然后在index.html中导入elastic.js文件,如下所示
<script src="js/elastic.js" type="text/javascript"></script>
之后把它注入到你的angular模块中,像这样:
angular.module('yourApp', ['monospaced.elastic']);
在你的HTML文件之后,在你的页脚栏这样做:
<ion-footer-bar style="height: auto; overflow: visible !important"><textarea rows="1" msd-elastic ng-model="myMsg">
</textarea>
- 如何使用jquery在填充自动完成的值后使文本框只读
- 使用Clipboard.js复制span文本
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 用程序搜索JQuery数据表中的文本
- jQuery匹配JSON对象的部分文本
- onkeyup无法动态创建多个文本区域
- 如何在下面的ES6循环中获得前面的文本
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 让文本输入幻灯片显示输入时的新文本输入?然后向后滑动
- 借助asp.net验证或java脚本对多个文本进行验证
- 无法在ajaxStart中更改跨度文本
- 高亮显示时编辑文本大小和颜色
- jquery中的文本框验证
- Javascript,有没有一种方法可以将数组写成没有逗号或空格的单个文本字符串
- Sails.js:同时发布文本输入和一个文件
- 当鼠标悬停在文本中的单词上时显示警报
- 是否有任何方法可以使用jQuery替换在数组中定义值的文本
- 将纯文本URL转换为可单击链接
- 在离子中自动生长文本区域
- 聚合物自动生长文本区域焦点()不工作