问题
I'm an angularJS newbie and i need to use ckeditor for a textarea in my app.
Before i've tried it on the angular app i've done a "html only" webpage. I've generated my ckeditor package here and added the needed tags (as below) and it works like a charm.
<!DOCTYPE HTML>
<html>
<head>
<title>CKEditor test page</title>
<meta charset="utf-8">
</head>
<body>
<script src='./ckeditor/ckeditor.js'></script>
<div>
<form>
<label>Text input</label>
<textarea name="text" id='editor1' class="ckeditor"></textarea>
<input type="submit" method="GET">
</form>
<script>
CKEDITOR.replace('editor1');
</script>
</div>
</body>
</html>
Now i'm trying to use ckeditor for a single text area of one of the tpl "views" for my angular project and i can't get it work :( Here's the content of the tpl.html view:
<script src='./ckeditor/ckeditor.js'></script>
<div class="container">
<div class="page-header">
<h2>Title</h2>
</div>
<form role="form" class="input_form">
<accordion>
<accordion-group heading="Add content" is-open="false">
<div class="form-group">
<label class="control-label">TEXT INPUT</label>
<textarea id='test' class="form-control" rows="3" ng-model="data.body"
placeholder="Write your text here!"/></textarea>
</div>
</accordion-group>
</accordion>
<div class="text-right">
<a ng-click="submitText()" class="btn btn-primary">Send text</a>
</div>
</form>
</div>
What is the most basic approach to get ckeditor working with an angularJS app? Could it be installing any of these directives? http://github.com/esvit/ng-ckeditor http://github.com/lemonde/angular-ckeditor
Any help would be welcome :)
回答1:
You could create your own directive like so:
(function () {
'use strict';
angular
.module('app')
.directive('ckeditor', Directive);
function Directive($rootScope) {
return {
require: 'ngModel',
link: function (scope, element, attr, ngModel) {
var editorOptions;
if (attr.ckeditor === 'minimal') {
// minimal editor
editorOptions = {
height: 100,
toolbar: [
{ name: 'basic', items: ['Bold', 'Italic', 'Underline'] },
{ name: 'links', items: ['Link', 'Unlink'] },
{ name: 'tools', items: ['Maximize'] },
{ name: 'document', items: ['Source'] },
],
removePlugins: 'elementspath',
resize_enabled: false
};
} else {
// regular editor
editorOptions = {
filebrowserImageUploadUrl: $rootScope.globals.apiUrl + '/upload',
removeButtons: 'About,Form,Checkbox,Radio,TextField,Textarea,Select,Button,ImageButton,HiddenField,Save,CreateDiv,Language,BidiLtr,BidiRtl,Flash,Iframe,addFile,Styles',
extraPlugins: 'simpleuploads,imagesfromword'
};
}
// enable ckeditor
var ckeditor = element.ckeditor(editorOptions);
// update ngModel on change
ckeditor.editor.on('change', function () {
ngModel.$setViewValue(this.getData());
});
}
};
}
})();
and to use it:
<!-- regular wysiwyg editor -->
<textarea ng-model="vm.article.Body" ckeditor></textarea>
<!-- minimal wysiwyg editor -->
<textarea ng-model="vm.article.Body" ckeditor="minimal"></textarea>
回答2:
Personally I like https://github.com/lemonde/angular-ckeditor. Step 1-3 are actually documented quite well in the README.md and the rest is pretty easy to develop...
- get the angular-ckeditor:
git clone -depth=50 https://github.com/lemonde/angular-ckeditor
- download resoureces via bower:
bower install angular-ckeditor
- Write yourself a controller (see example in the README.md)
- link your js files in your html, keep track of the order:
<script type="text/javascript" src="/angular-ckeditor/angular-ckeditor.min.js"></script>
<script type="text/javascript" src="/angular-ckeditor/bower_components/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="/yourApp.js"></script>
<script type="text/javascript" src="/yourController.js"></script>
- use your controller and the directive inside your html as described. If you would like to pre-initialize your editor with data from your html, you could use something like this:
data-ng-init="content='this is a text with <a href="http://localhost/whatever.html" target="_blank">an embedded Link</a>'" contenteditable="true" ready="onReady()">
or you init your model variable(s) in your controller (hardcoded or via $http
).
回答3:
I just created an angular component (https://github.com/jziggas/ng-ck) for ckeditor that you can try using. It is compatible with Angular 1.6 and CKEditor 4.6. Once you install the ckeditor library itself, using the component is as simple as <ng-ck ng-model="content"></ng-ck>
. There are also callbacks available in the documentation that hook right into ckeditor's eventing.
来源:https://stackoverflow.com/questions/28429105/how-to-use-ckeditor-in-angular-js-app