如何在Angular JS中使用Colorbox

时间:2013-02-01 07:44:20

标签: angularjs colorbox

我如何使用Angular JS使用colorbox(也欢迎使用fancybox或lightbox),如果我为它编写指令,是否还有其它方法。

这是我的HTML:

...
<h2 class="box-header">
    <i class="icon-hdd"></i>    
    <div class="btn-group pull-right">      
        <a class="btn btn-mini">
            <i class="icon-zoom-in"></i>
            Expand
        </a>
    </div>
</h2>   
<div id="open-with-colorbox">
...
</div>
...

用户将单击展开按钮,并且具有open-with-colorbox id的div将打开为彩色框。

PS:我是Angular JS的新手,这也是我寻找如何使用colorbox的解决方案的原因,我在我的应用程序中使用了twitter bootstrap。

2 个答案:

答案 0 :(得分:15)

写一个指令,这是最好的选择。

指令示例:

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

HTML:

<a colorbox="{transition:'fade'}" href="...">Image</a>

答案 1 :(得分:3)

我在我的代码中尝试了bmleite指令,但它不起作用。 cboxElement类已添加到元素中,但colorbox在单击时未打开。最后,我发现这个指令很有魅力。

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);      
            })
          }
        });
      });
    }
  };
});

然后在你的html部分中使用它:

<img src="path_to_image" colorbox="path_to_large_image" />