maxlength 不适用于 ckeditor angularjs 指令的 textarea [英] maxlength not working in textarea for ckeditor angularjs directive
问题描述
我用 ckeditor 插件在 angularjs 中创建了一个应用程序,我为 ckeditor 创建了一个指令,该应用程序工作正常,但问题是我需要将最大字符长度设置为 50,所以我把 maxlength="50"
,但它不起作用,
谁能告诉我一些解决方案
html
<h3>CKEditor 4.2:</h3><div ng-repeat="ckEditors 中的编辑器"><textarea data-ng-model="editor.value" maxlength="50" data-ck-editor></textarea><br/><button ng-click="addEditor()">新编辑器</button>
脚本
var app = angular.module('app', []);app.directive('ckEditor', [function () {返回 {要求:'?ngModel',链接:函数($scope,elm,attr,ngModel){var ck = CKEDITOR.replace(elm[0]);ck.on('pasteState', function () {$scope.$apply(function () {ngModel.$setViewValue(ck.getData());});});ngModel.$render = 函数(值){ck.setData(ngModel.$modelValue);};}};}])函数 myCtrl($scope){$scope.ckEditors = [{value: ''}];}
您需要将 id
添加到您的 textarea
中,如下所示:
你需要处理CKEDITOR
的关键事件:
window.onload = function() {CKEDITOR.instances.mytext.on('key', function() {var str = CKEDITOR.instances.mytext.getData();如果(字符串长度> 50){CKEDITOR.instances.mytext.setData(str.substring(0, 50));}});};
这有效,但是请注意,内容也包含 html 标签,您可能希望保留它们,
I have created an application in angularjs with ckeditor plugin, I have created a directive for ckeditor, The application is working fine but the issue is that i need to set a max character length to be 50, so i put maxlength="50"
, but its not working,
Can anyone please tell me some solution for this
html
<div data-ng-app="app" data-ng-controller="myCtrl">
<h3>CKEditor 4.2:</h3>
<div ng-repeat="editor in ckEditors">
<textarea data-ng-model="editor.value" maxlength="50" data-ck-editor></textarea>
<br />
</div>
<button ng-click="addEditor()">New Editor</button>
</div>
script
var app = angular.module('app', []);
app.directive('ckEditor', [function () {
return {
require: '?ngModel',
link: function ($scope, elm, attr, ngModel) {
var ck = CKEDITOR.replace(elm[0]);
ck.on('pasteState', function () {
$scope.$apply(function () {
ngModel.$setViewValue(ck.getData());
});
});
ngModel.$render = function (value) {
ck.setData(ngModel.$modelValue);
};
}
};
}])
function myCtrl($scope){
$scope.ckEditors = [{value: ''}];
}
You need to add an id
to your textarea
, like this:
<textarea data-ng-model="editor.value" maxlength="50" id="mytext" data-ck-editor></textarea>
You need to handle the key events for CKEDITOR
:
window.onload = function() {
CKEDITOR.instances.mytext.on( 'key', function() {
var str = CKEDITOR.instances.mytext.getData();
if (str.length > 50) {
CKEDITOR.instances.mytext.setData(str.substring(0, 50));
}
} );
};
This works, however, note, that the content contains html tags as well, you might want to keep them,
这篇关于maxlength 不适用于 ckeditor angularjs 指令的 textarea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!