【问题标题】:AngularJS Fancybox PopupAngularJS Fancybox 弹出窗口
【发布时间】:2014-04-14 09:57:01
【问题描述】:

我已经开始了一个angularjs项目,我想实现fancybox。

为此,我在解决方案中包含了 jQuery 和 fancybox 插件。我正在尝试在下面的代码中打开模板,在花式框窗口中显示。

查看

<a href="" ng-click="openPage('popups/add.html')">ADD</a>

控制器

app.controller('MainController',
    function MainController($scope) {
        $scope.user = "Hey Welcome";

        $scope.open = function(template_path){
            $.fancybox({"href":template_path})
        }
    }
)

还有popup/add.html

<div class="pop-contnr">
    <h2>ADD</h2>
    <table>
        <thead>
            <tr>
                <th align=center>{{user}}</th>
            </tr>
        </thead>
    </table>
</div>

Fancybox 成功打开一个包含模板的窗口,但尚未计算 {{user}} 表达式。有人可以帮忙吗?

【问题讨论】:

    标签: angularjs fancybox


    【解决方案1】:

    我已经为 fancybox 创建了一个指令

    app.directive('fancybox',function($compile, $timeout){
        return {
            link: function($scope, element, attrs) {
                element.fancybox({
                    hideOnOverlayClick:false,
                    hideOnContentClick:false,
                    enableEscapeButton:false,
                    showNavArrows:false,
                    onComplete: function(){
                        $timeout(function(){
                            $compile($("#fancybox-content"))($scope);
                            $scope.$apply();
                            $.fancybox.resize();
                        })
                    }
                });
            }
        }
    });
    

    【讨论】:

    • 如果您也包含了 html 示例,它将很有用。
    【解决方案2】:

    这是我和我的团队编写的一个 fancybox 指令的简化版本,它通过单击打开基于模板的 fancybox。

    在标记中调用它是这样的:

    <div fancybox ng-click="openFancybox('templateUrl')"> </div>
    

    指令的代码是:

    app.directive('fancybox', function ($compile, $http) {
        return {
            restrict: 'A',
    
            controller: function($scope) {
                 $scope.openFancybox = function (url) {
    
                    $http.get(url).then(function(response) {
                        if (response.status == 200) {
    
                            var template = angular.element(response.data);
                            var compiledTemplate = $compile(template);
                            compiledTemplate($scope);
    
                            $.fancybox.open({ content: template, type: 'html' });
                        }
                    });
                };
            }
        };
    });
    

    可以看到在这个plunker工作

    【讨论】:

    • 我不想在每次点击元素时都加载模板 - 是否有可能只加载一次该模板?
    • 好东西,非常感谢!看起来 Angular 的内部模板加载器(在指令中)在初始化时遇到了 fancybox 识别的问题......这种方法保证模板真的在那里。
    • @Tony - 浏览器将缓存模板,因此将节省 HTTP 往返行程。
    • 不使用 jquery 可以做到这一点吗?
    • @pandudas - Fancybox 基于 jQuery,所以没有它就无法工作。这是docs。您必须为不基于 jquery 的模式使用不同的框架。
    【解决方案3】:

    我扩展了 answer above 以使用 Angular 的模板缓存。

    在标记中调用它是这样的:

    <div fancybox fancybox-template="template.html">Open Fancybox</div>
    

    指令的代码是:

    app.directive('fancybox', function ($templateRequest, $compile) {
        return {
            scope: true,
            restrict: 'A',
            controller: function($scope) {
                $scope.openFancybox = function (url) {
                    $templateRequest(url).then(function(html){
                        var template = $compile(html)($scope);
                        $.fancybox.open({ content: template, type: 'html' }); 
                    });
                };
            },
            link: function link(scope, elem, attrs) {
                elem.bind('click', function() {
                    var url = attrs.fancyboxTemplate;
                    scope.openFancybox(url);
                });
            },
        }
    });
    

    这是plunker

    【讨论】:

      猜你喜欢
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多