N
N
nfuture2014-04-01 16:45:24
Angular
nfuture, 2014-04-01 16:45:24

How to get text inside AngularJS directive tag?

Hello.
There is no way to get the text inside the directive tag.
I have html code

<div data-keditor>Lorem ipsum dolor sit amet...</div>

Directive code:
myapp.directive("keditor", function () {
    return {
        template: '<textarea rows="10" cols="30" style="height:200px;" ></textarea>',
        replace: true,
        scope: {},
        link: function(scope, element, attributes) {	
               console.log("где-то здесь нужно вернуть текст 'Lorem ipsum dolor sit amet...'");

               // замена происходит правильно
    element.text("test");

        }
    }
});

You need to get the text Lorem ipsum dolor sit amet... , and then write element.text(/* text*/); in textarea

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-04-01
Protko @Fesor

why not do that?

myapp.directive("keditor", function () {
    return {
        template:  function (el, attr) {
            return '<textarea rows="10" cols="30" style="height:200px;" >'+el.text()+'</textarea>';
        },
        replace: true,
        scope: {},
        link: function(scope, element, attributes) {	
               console.log("где-то здесь нужно вернуть текст 'Lorem ipsum dolor sit amet...'");

               // замена происходит правильно
    element.text("test");

        }
    }
});

Otherwise, you need to access the element at the compile stage.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question