AngularJS binding jQuery qTip2 plugin
Asked Answered
I

1

11

I am trying to figure out how to bind the content of a tooltip with angular. I have a directive that looks like this:

script.js

var myApp = angular.module('myApp', []);

myApp.directive('initToolbar', function(){
    return {
        restrict: 'A',
        link: function(scope, element, attrs)
        {
            $(element).qtip({
                content: {
                    ajax:
                    {
                        url: 'button.html'
                    }
                },
                position: {
                    my: 'bottom left',
                    at: 'bottom middle',
                    target: $(element)
                },
                hide: {
                    fixed : true,
                    delay : 1000
                }
            });
        }
    }
});

It uses the qTip2 plugin from here

My index.html looks like this (please note that in the actual file I have included all the sources in head, I am just not pasting it here to avoid clutter):

<body>
    <div initToolbar>
        <p>
            Hover over me. Hover over me. Hover over me.
        </p>
    </div>
</body>

and

button.html

<div ng-controller="myController">
    <button ng-click="someFunction()">Click me</button>
</div>

As you can see in the directive code. button.html is loaded into the tooltip, however this prevents angular from functioning properly-- The ng-click does not work when button.html is loaded into the popup. That is because angular does not know about it.

I also know that button.html is valid because simply adding

<ng-include src="'button.html'"> 

to index.html works fine (i.e clicking on the button executes someFunction())

So my question is:

How can I bind the actual content of the tooltip with angular? If not the content, is there a way to bind the tooltip so angular knows about it? I am familiar with $scope.$apply() but I am not quite sure how to use it here.

Inspiratory answered 29/7, 2013 at 21:14 Comment(1)
Hey I updated my answer with a working plunkr. Hopefully it still helps you. Plunkr doesn't work at my office :-(Askew
A
15

UPDATE 1 Make sure to go from snake-case to camelCase when going from HTML to javascript in angular. So init-toolbar in html translates to initToolbar in javascript.

Here is a working sample: http://plnkr.co/edit/l2AJmU?p=preview

HTML

<div init-toolbar="">
  <p>
    Hover over me. Hover over me. Hover over me.
  </p>
</div>

Button.html

<div>
  <button ng-click="someFunction()">Click me</button>
</div>

JAVACRIPT

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.someFunction = function() {
    $scope.name = 'FOO BAR';
  };
});

app.directive('initToolbar', function($http, $compile, $templateCache){
    return {
        restrict: 'A',
        link: function(scope, element, attrs)
        {
          $http.get('button.html', {cache: $templateCache}).
            success(function(content) {
              var compiledContent = $compile(content)(scope);

              $(element).qtip({
                content: compiledContent,
                position: {
                  my: 'bottom left',
                  at: 'bottom middle',
                  target: $(element)
                },
                hide: {
                  fixed : true,
                  delay : 1000
              }
            });

          });

        }
    }
});

ORIGINAL

The reason the button does not work is because angular does not know it should bind to it. You tell angular to do that using $compile. I don't know much about that qTip2 pluggin, but if you load the template, then compile it $compile(template)(scope); then hand it over to qTip2, you will get the results you expect.

Askew answered 30/7, 2013 at 13:38 Comment(4)
If possible can you maybe give a small code snippet. I went through angular docs and they always call $compile on a template that is defined in the directive. In this case I am referring to a jqueryObject (qTip) which is not necessarily a template and also the inner content points to a url (button.html). Sorry I am not quite clear on $compile and how to use it yet.Inspiratory
Yeah I was trying to make a plunkr but for some reason its down. I'll check later and try to post something.Askew
From what I understand since qTip is applied to the element using jQuery, changes to the qtip object on runtime will not be applied. For example if we changed at: bottom middle => at: top right (by passing an argument to the directive perhaps) will not have an effect.Pattison
@mxa055 I'm not sure what you mean. If you just want to set a config property on the directive, you can do that through attributes. If you want to dynamically change it at runtime, you have to use a $scope.$watchAskew

© 2022 - 2024 — McMap. All rights reserved.