0

I'm novice to angularjs. I've a problem with Popover jquery plugin, I've created a directive for it and don't know why it's not working correctly

here is a plunker link

1 Answer 1

1

Your binding is read without evaluation against the current scope. You could do evaluation manually using scope.$eval.

Try reading the data as text and evaluate manually:

var api = scope.$eval($(this).attr('data-api'));

DEMO

Another solution using $timeout to schedule the function to the next phase to ensure that angular has finished its bindings => we don't need to use scope.$eval anymore:

app.directive('popover', function($timeout) {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
      $timeout(function(){
          element.popover({
          trigger: 'hover',
          html: true,
          content: function() {
            var api = angular.fromJson(attrs.api);

            return (
              '<ul><li>' + api[0] + ',' + api[1] + '</li><li>' + api[2] + ',' + api[3] + '</li>');
          }
        });
      });
    }
  };
});

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but why the title still have problem?
@Ahmed Hashem: I added another solution, I think it's better to let angular do the bindings, we avoid doing it ourselves which may conflict with angular bindings and create unpredictable results.

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.