7

I am running into small issue that I can't seem to figure out. The code below works, except for the first time you hover over the link. Any help is greatly appreciated.

http://jsfiddle.net/LpK6d/1/

<div ng-app="myApp">
<a
    class="pop-over"
    data-original-title="default value"
    data-placement="top">test link</a>
</div> 
var app = angular.module("myApp", []);

app.directive('popOver', function($http) {
  return {
    restrict: 'C',
    link: function(scope, element, attr) {

        element.bind('mouseover', function(e) {
            $http.get("http://ip.jsontest.com/?callback=someFunction")
              .success(function(data) {
                 attr.$set('originalTitle', data);
                 element.tooltip();                  
              });
            })
        }
    }
});
3
  • Does it need to load on each mouseover? Would this work: jsfiddle.net/ZsMY4 ? Commented May 21, 2013 at 2:18
  • @jkoreska It does need to load on every mouseover, because I use the data for the tooltip. Commented May 21, 2013 at 16:38
  • Ok, you might consider caching it at least. How's this: jsfiddle.net/ZsMY4/1 ? Commented May 21, 2013 at 18:18

1 Answer 1

4

Ok so calling .tooltip() before the AJAX call might seem like a good idea, but the method copies the data-original-title or 'title'into its internal variables so we can't change it :(.

the thing is that calling tooltip() it just listens to onmouseover events. You have to call .tooltip('show') to make it appear programmatically.

Here is the working fiddle : http://jsfiddle.net/rB5zT/49/

This is the correct way to do it:

var app =  angular.module("myApp", [])
app.directive('popOver', function($http) {
 return {
  restrict: 'C',
  link: function(scope, element, attr) {
    var i =0;
    element.tooltip();
    $(element).bind('mouseover',function(e) {

        $http.get("test").success(function(){
           attr.$set('originalTitle', "Some text "+i++);  
            element.tooltip('show');
         });
        });
   }
 }
});
Sign up to request clarification or add additional context in comments.

5 Comments

the problem is that first time you hover over the tooltip, it displays default text. My goal was to load the content via xhr every time someone hover over the link.
OK, here is the way to do it. Might seem like a work-around, but you do have to do a http request to get the actual value of the tooltip. Hope it helps
This is the working version. You have to call tooltip('show') on ajax response :).
@Shriram, thanks for pointing out the bug. The fiddle URLs for getting bootstrap js and css were not working anymore. They were redirecting to getboostrap.com.
Don't forget fixTitle... otherwise it won't work. $(element).bind('mouseover', function (e) { element.attr('title', element.title).tooltip('fixTitle').tooltip('show'); });

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.