Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Magnus Olsson 25 posts 71 karma points
    May 12, 2014 @ 21:26
    Magnus Olsson
    0

    Setting RTE value in a custom property editor

    Hi,

    Im trying to create a property editor where I have multiple strings (in different languages) in my value. The aim is to be able to edit these with the RTE which I have successfully been able to put in my prop editor. But I can't seem to manipulate the value of the editor from code. Either bind the editor value to a $scope.xxxx or by re-setting the $scope.myEditor.value from code after the umb-editor has been placed on my page. 

    Im really new to Angular so perhaps I'm doing this wrong. 

    Any pointers would be appreciated.

    //Magnus

    <div ng-controller="localizedEditor">
    
        <!--print out for debug purposes-->
        <div>{{selectedLang.lang}}</div>
        <div>{{myProperty.value}}</div>
    
        <!--lang selector-->
        <select ng-model="selectedLang" ng-options="l as l.lang for l in model.value" ng-change="loadLang()"/>
    
        <!--editor-->
        <ng-form>
            <umb-editor model="myEditor"></umb-editor>
            <pre>
                Value from the RTE {{myEditor.value}}
            </pre>
        </ng-form>
    </div>

     

    And my controller script. (the two ways I tried to make this work have comments in bold)

    (function () {
        'use strict';
    
        angular.module('umbraco').controller('localizedEditor', ['$scope', localizedEditor]);
    
        function localizedEditor($scope) {
    
            // create some sample value items
            if (!$scope.model.value) {
                $scope.model.value = [{
                    lang: "en",
                    text: "This is an english text"
                }, {
                    lang: "sv",
                    text: "En svensk text"
                }, {
                    lang: "no",
                    text: "Dette er en norsk tekst"
                }];
            }
    
            // pre-select english
            $scope.selectedLang = $scope.model.value[0];
    
            // set up the RTE
            $scope.myEditor = {
                view: 'rte',
                config: {
                    editor: {
                        toolbar: ["code", "undo", "redo", "cut", "styleselect",
                                "bold", "italic", "alignleft", "aligncenter",
                                "alignright", "bullist", "numlist", "link",
                                "umbmediapicker", "table", "umbembeddialog"],
                        stylesheets: [],
                        dimensions: { height: 400, width: 400 }
                    }
                },
    
                // THIS DOESN'T SEEM TO WORK
                value: $scope.selectedLang.text
            };
    
            // on selector change, load the new language
            $scope.loadLang = function () {
                angular.forEach($scope.model.value, function (item, i) {
                    if (item.lang === $scope.selectedLang.lang) {
                        console.log("setting " + item.lang);
                        $scope.selectedLang = item;
    
                        // THIS ALSO DOESN'T SEEM TO WORK (WELL)
                        $scope.myEditor.value = item.text;
                    }
                });
            };
        }
    })();
    
  • aits 15 posts 46 karma points
    Mar 01, 2016 @ 09:39
    aits
    0

    Have you managed this?

    Can anybody has the solution for this.?

  • Avatar 15 posts 92 karma points
    Oct 02, 2016 @ 19:31
    Avatar
    1

    Came across this while looking for a solution myself. You're about halfway there. I managed to get somthing similar working on Umbraco 7.4.3.

    When you are first checking for $scope.model.value, be sure to also check if it is an empty string, because it could exist, and thus never setup your model.value as you defined it.

    When setting up the RTE, the value here defines what to populate the RTE with when it first initializes. You want to make sure that there is a value for $scope.model.value[0].text, otherwise you can expect an empty text editor.

    // set up the RTE
        $scope.myEditor = {
            view: 'rte',
            ...
            //Defines initial RTE content
            value: $scope.model.value[0].text
        };
    

    As you are entering content into the RTE, it is being stored in $scope.myEditor.value, so you'll want to set up a $watch on that and update your stored value. This will set it up so that the RTE content will be constantly updating your $scope.model.value as it changes.

    $scope.$watch('myEditor.value', function() {
        // Sets the stored value with the contents of the RTE
        $scope.model.value[0].text = $scope.myEditor.value;
    })
    

    It looks like you are setting up for multiple values to be edited by the same RTE instance, so you'll want to setup some sort of handling between the different values and $scope.selectedLang as well.

Please Sign in or register to post replies

Write your reply to:

Draft