1

I'm experiencing an issue trying to use tinymce API inside an angular directive in JSFiddle. Here is the example The tinymce editor is initialised just fine, there's no errors in browser console. But I get 'undefined' if I try to get an instance of the tinymce Editor. The question is: why does tinymce.get(id); result in undefined?

HTML:

<div ng-app="myApp">
    <div ng-controller="MainCtrl">
        <my-editor ng-model="text"></my-editor>
    </div>
</div>

JS:

var app = angular.module('myApp', []);

app.controller('MainCtrl', function($scope) {

});
app.directive('myEditor', function () {
    var uniqueId = 0;
    return {
        restrict: 'E',
        require: 'ngModel',
        scope: true,
        template: '<textarea></textarea>',
        link: function (scope, element, attrs, ngModel) {
            var id = 'myEditor_' + uniqueId++;
            element.find('textarea').attr('id', id);
            tinymce.init({
                selector: '#' + id
            });

            var editor = tinymce.get(id);
            alert(editor); **// why is this undefined?**
        }
    }
});

I've also played with options in Frameworks & Extensions section of JSFiddle. But with no success.

4

3 回答 3

2

您正在处理这个问题,当您发出警报时,元素尚未附加到 dom 中。(看萤火虫中的html)

<my-editor ng-model="text" class="ng-scope ng-pristine ng-valid">
    <textarea id="myEditor_0"></textarea>
</my-editor>

将警报放在 a 内setTimeout将允许您访问alert()该对象。

setTimeout(function(){
     var editor = tinymce.get(id);
     alert(editor); // why is this undefined?
},0);
于 2013-07-08T14:34:16.887 回答
1

如果您使用的是angular-ui-tinymce,正确的方法是在 in 或 in 中设置init_instance_callback 选项tinyMCE.inittinymceOptions

$scope.tinymceOptions = {
  init_instance_callback : function(editor) {
     MyCtrl.editor=editor
     console.log("Editor: " + editor.id + " is now initialized.");
     editor.on('Change', function(editor, e) {
        MyCtrl.func()
     });             
}
于 2014-11-28T02:16:33.193 回答
0

除了马克给出的答案:

您需要等待编辑器初始化并准备好使用。为此,您可以使用 onInit tinymce 处理程序。当编辑器准备好时, onInit 会被触发。

于 2013-07-25T11:05:47.757 回答