0

I'm working on creating an angular directive (element) that will apply some transformation to the text within.

Here is the directive:

module.directive('myDir', function() {
    return {
      restrict: 'E',
      link: function(scope, elem, attrs) {
        console.log(elem.text());       
      },
    };
  });

For now I've just placed a console.log in there to make sure I am seeing the expected text.

If my html is like this it works as expected:

<my-dir>Hello World!</my-dir>

However, if I use a variable from the scope such as this:

<my-dir>{{myMessage}}</my-dir>

Then that is the text I seen in the console output instead of the variables value. I think I understand why the output is this way, however I'm not sure how to get the correct value. The requirement is that the directive should be able to transform text from both examples.

Thoughts?

0

4 Answers 4

2

Use $interpolate service to convert the string to a function, and apply the scope as parameter to get the value (fiddle):

module.directive('myDir', function($interpolate) {
    return {
      restrict: 'E',
      link: function(scope, elem, attrs) {
        console.log($interpolate(elem.text())(scope));
      },
    };
  });
Sign up to request clarification or add additional context in comments.

4 Comments

Same as that of mine.. :)
Though I did +1 for you my friend. :) Here we are not to win OR lose..just here to share & earn knowledge..
It goes both ways :-P
Had to give it to you for including links to the interpolate service and a fiddle example. If I could have marked Pankaj's as well I would have. Thanks to you both.
2

If you really interested get the interpolated content then do use $interpolate service to evaluated the interpolated content.

Code

link: function(scope, elem, attrs) {
    console.log($interpolate(elem.text())(scope));
},

Don't forget to add $interpolate as dependency on directive function.

Comments

0

Have a look on $compile or http://odetocode.com/blogs/scott/archive/2014/05/07/using-compile-in-angular.aspx

This should work:

module.directive('myDir', function($compile) {
  return {
    restrict: 'E',
    link: function(scope, elem, attrs) {
      console.log($compile(elem.text())(scope));       
    },
  };
});

Comments

0

I would suggest you use an isolated scope on directive which will give you access to the value without having to get it from the dom. You will also be able to manipulate it directly in the link function as part of scope

<my-dir my-message="myMessage"></my-dir>

JS

module.directive('myDir', function() {
    return {
      restrict: 'E',
      template: '{{myMessage}}',
      scope:{
        myMessage: '=', 
      },
      link: function(scope, elem, attrs) {
        console.log(scope.myMessage);       
      },
    };
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.