TinyMCE <textarea> two way bound with AngularJS

前端 未结 3 1180
被撕碎了的回忆
被撕碎了的回忆 2021-01-06 23:55

Is it possible to apply two way binding to a that has had TinyMCE applied to it for Rich Text Formatting.

I can\'t get

3条回答
  •  轮回少年
    2021-01-07 00:31

    You can do this by creating your own directive.

    What you need to do is to let your directive sync your model when something in the TinyMCE editor changes. I have not used TinyMCE, but Wysihtml5. I think you can remake this to use TinyMCE instead.

    angular.module('directives').directive('wysihtml5', ['$timeout',
    function ($timeout) {
        return {
            restrict: 'E',
            require: 'ngModel',
            template: "", // A template you create as a HTML file (use templateURL) or something else...
            link: function ($scope, $element, attrs, ngModel) {
    
                // Find the textarea defined in your Template
                var textarea = $element.find("textarea");
    
                // When your model changes from the outside, use ngModel.$render to update the value in the textarea
                ngModel.$render = function () {
                    textarea.val(ngModel.$viewValue);
                };
    
                // Create the editor itself, use TinyMCE in your case
                var editor = new wysihtml5.Editor(textarea[0],
                    {
                        stylesheets: ["/style.css"],
                        parserRules: wysihtml5ParserRules,
                        toolbar: true,
                        autoLink: true,
                        useLineBreaks: false,
                    });
    
                // Ensure editor is rendered before binding to the change event
                $timeout(function () {
    
                    // On every change in the editor, get the value from the editor (textarea in case of Wysihtml5)
                    // and set your model
                    editor.on('change', function () {
                        var newValue = textarea.val();
    
                        if (!$scope.$$phase) {
                            $scope.$apply(function () {
                                ngModel.$setViewValue(newValue);
                            });
                        }
                    });
    
                }, 500);
            }
        };
    }]);
    

    Then you can use the directive in your html page like this:

    
    

    Here's a link if you need more info on creating your own directive: http://docs.angularjs.org/guide/directive

提交回复
热议问题