0

I'm trying to use a jQuery plugin that can curve text (Arctext.js) as an Angular Directive.

The Directive seems to work fine, the given text is curved as expected, except that I want this text to be an Angular variable. And the problem is the jQuery plugin seems to be executed before Angular resolves the variable value.

My html element :

<h2 class="circle">{{ myController.myVar }}</h2>

My directive :

return {
    restrict: "C",
    link: function(scope, element, attrs) {
        element.arctext({radius: 500})
    }
}

The page displays '{{ myController.myVar }}' as curved text.

How to get the variable value instead ?

3
  • 1
    Will you share more code or prepare a fiddle? Commented Jan 25, 2017 at 10:09
  • looks like your directive is not able to access plugin. Did you try to get plugin data inside directive.? Commented Jan 25, 2017 at 10:14
  • Here's a fiddle : jsfiddle.net/k8ffmbL0/1 Commented Jan 25, 2017 at 11:13

1 Answer 1

1

Try this:

HTML:

<h2 class="circle" text="{{ myController.myVar }}"></h2>

Directive:

return {
    restrict: 'C',
    link: function(scope, element, attrs) {
        attrs.$observe('text', function(newval) {
            if (newval) {
                $(element).text(newval).arctext({radius: 500})
            }
        });
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

The title is not showing. I suppose the arctext plugin use the .text() method to get the string inside element. How to apply the plugin to the 'text' attribute in your example ?
@BenjaminLemoine I update the answer. see fiddle here
Ok but it doesn't work on my project. I don't know why... When the page is loading for the first time (ie: refresh button) the title appears correctly but not curved (like if the plugin is not instanced). If I click on a link and then I go back to this page, the title is correctly displayed and curved. It's like, at the first time, the plugin is not initialized yet. I can't reproduce the problem on fiddle...
The $observe element was fired twice. Testing if the attribute value is not empty before using the jQuery function solved my problem.

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.