How to use Colorbox with Angular JS

后端 未结 2 1613
小蘑菇
小蘑菇 2021-02-05 16:42

How can I use colorbox (fancybox or lightbox is welcome too) with Angular JS, should I write a directive for it is there any other methods for it.

Here is my HTML:

相关标签:
2条回答
  • 2021-02-05 17:21

    Write a directive, it's the best option.

    Directive example:

    app.directive('colorbox', function() {
      return {   
        restrict: 'AC',    
        link: function (scope, element, attrs) {        
          $(element).colorbox(attrs.colorbox);     
        }
      };  
    });
    

    HTML:

    <a colorbox="{transition:'fade'}" href="...">Image</a>
    
    0 讨论(0)
  • 2021-02-05 17:27

    I have tried bmleite directive in my code but it doesnt work. The cboxElement class added to the element but colorbox didnt open when clicked. Finally i found this directive which worked like a charm.

    app.directive('colorbox', function($compile, $rootScope){
      return {
        link: function(scope, element, attrs){
          element.click('bind', function(){
            $.colorbox({
              href: attrs.colorbox,
              onComplete: function(){
                $rootScope.$apply(function(){
                  var content = $('#cboxLoadedContent');
                  $compile(content)($rootScope);      
                })
              }
            });
          });
        }
      };
    });
    

    Then in your html section use it with:

    <img src="path_to_image" colorbox="path_to_large_image" />
    
    0 讨论(0)
提交回复
热议问题