将 Fancybox 插件转换为 Angular 指令
converting Fancybox plugin to Angular directive
我正在使用 ng-repeat
将 url 放置到 iframe {{item.details}} 在每个按钮中,它工作正常。
HTML:
<a class="various small_button shadow none" fancybox ng-href="{{item.details}}">View Details</a>
问题是为 fancybox
创建指令以在单击按钮时创建 lightbox iframe
。我试图通过查看这些帖子 post1 and post 2 和其他一些帖子来解决这个问题,它们很有帮助,但在这种情况下不是解决方案。
Fancybox 指令:
app.directive('fancybox', function(){
return{
restrict: 'e',
link: function(scope, element, attrs){
element.fancybox({
type :'iframe',
scrolling : 'no',
maxWidth : 800,
maxHeight : 400,
fitToView : true,
width : '70%',
height : '70%',
autoSize : false,
closeClick : true,
openEffect : 'none',
closeEffect : 'none',
source :function(current){
return $(current.element);
}
});
}
}
});
使用 jQuery 我调用 fancybox
使用 类“.various”和“.ad”。我想仅使用 angular 以相同的方式调用 fancybox
。
原创花式 jQuery:
jQuery(document).ready(function(){
//fancybox window script
$(".various").fancybox({
type :'iframe',
scrolling : 'no',
maxWidth : 800,
maxHeight : 400,
fitToView : true,
width : '70%',
height : '70%',
autoSize : false,
closeClick : true,
openEffect : 'none',
closeEffect : 'none',
});
$(".ad").fancybox({
maxWidth : 210,
maxHeight : 140,
fitToView : true,
width : '100%',
height : '100%',
autoSize : false,
closeClick : true,
openEffect : 'none',
closeEffect : 'none'
});
});//set up close
您可以使用:
app.directive('fancybox', function(){
return {
restrict: 'A',
link: function(scope, element, attrs){
$(element).fancybox({
type :'iframe',
scrolling : 'no',
maxWidth : 800,
maxHeight : 400,
fitToView : true,
width : '70%',
height : '70%',
autoSize : false,
closeClick : true,
openEffect : 'none',
closeEffect : 'none'
});
}
}
});
属性 restrict
必须是 A
(大写),因为在您的示例中指令是一个属性。 E
(大写)表示指令是一个元素。您可以使用组合:restrict: 'AE'
。 More information
您需要使用 $(element).fancybox({ ... })
,因为 fancybox 是一个 jquery 插件。
我正在使用 ng-repeat
将 url 放置到 iframe {{item.details}} 在每个按钮中,它工作正常。
HTML:
<a class="various small_button shadow none" fancybox ng-href="{{item.details}}">View Details</a>
问题是为 fancybox
创建指令以在单击按钮时创建 lightbox iframe
。我试图通过查看这些帖子 post1 and post 2 和其他一些帖子来解决这个问题,它们很有帮助,但在这种情况下不是解决方案。
Fancybox 指令:
app.directive('fancybox', function(){
return{
restrict: 'e',
link: function(scope, element, attrs){
element.fancybox({
type :'iframe',
scrolling : 'no',
maxWidth : 800,
maxHeight : 400,
fitToView : true,
width : '70%',
height : '70%',
autoSize : false,
closeClick : true,
openEffect : 'none',
closeEffect : 'none',
source :function(current){
return $(current.element);
}
});
}
}
});
使用 jQuery 我调用 fancybox
使用 类“.various”和“.ad”。我想仅使用 angular 以相同的方式调用 fancybox
。
原创花式 jQuery:
jQuery(document).ready(function(){
//fancybox window script
$(".various").fancybox({
type :'iframe',
scrolling : 'no',
maxWidth : 800,
maxHeight : 400,
fitToView : true,
width : '70%',
height : '70%',
autoSize : false,
closeClick : true,
openEffect : 'none',
closeEffect : 'none',
});
$(".ad").fancybox({
maxWidth : 210,
maxHeight : 140,
fitToView : true,
width : '100%',
height : '100%',
autoSize : false,
closeClick : true,
openEffect : 'none',
closeEffect : 'none'
});
});//set up close
您可以使用:
app.directive('fancybox', function(){
return {
restrict: 'A',
link: function(scope, element, attrs){
$(element).fancybox({
type :'iframe',
scrolling : 'no',
maxWidth : 800,
maxHeight : 400,
fitToView : true,
width : '70%',
height : '70%',
autoSize : false,
closeClick : true,
openEffect : 'none',
closeEffect : 'none'
});
}
}
});
属性 restrict
必须是 A
(大写),因为在您的示例中指令是一个属性。 E
(大写)表示指令是一个元素。您可以使用组合:restrict: 'AE'
。 More information
您需要使用 $(element).fancybox({ ... })
,因为 fancybox 是一个 jquery 插件。