Miqdad Ali Miqdad Ali - 2 months ago 124
AngularJS Question

AngularJS Fancybox Popup

I have started an angularjs project and I would like to implement fancybox.

For this, I have included the jQuery and fancybox plugins to the solution. I am attempting to open the template in the code shown below in a fancybox window.

View

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


Controller

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

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


And popup/add.html

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


Fancybox successfully opens a window containing the template, but the
{{user}}
expression has not been evaluated. Can anyone please help?

Answer

I have created a directive for 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();
                    })
                }
            });
        }
    }
});