Search code examples
jqueryjsonangularjscolorbox

AngularJS, JSON & JQuery Colorbox Issue


I am not able to get colorbox working as expected (image opens in overlay) when using angularjs / json to populate the data. I have tried various solutions including the order of the javascript, etc. Any thoughts?

index.html File:

<!doctype html>
<html lang="en" ng-app="portfolioApp" ng-controller="PortfolioCtrl">
<meta charset="UTF-8">
<title>JQUERY COlORBOX & ANGULARJS JSON</title>

<link href='//cdnjs.cloudflare.com/ajax/libs/jquery.colorbox/1.4.33/example1/colorbox.css' rel='stylesheet' type='text/css'>

<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.8/angular.js"></script>
<script src="controllers.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.colorbox/1.4.33/jquery.colorbox.js"></script>

<script>
$(document).ready(function(){
$(".portfolio-gallery").colorbox({rel:'portfolio-gallery', slideshow:true});
});
</script>

</head>

<body>


<div ng-repeat="portfolio in portfolios" class="{{portfolio.sideclass}}">

    <a class="portfolio-gallery" ng-href="{{portfolio.url}}" title="{{portfolio.title}}">
        <img ng-src="{{portfolio.urlthumb}}" width="180px" height="160px" alt="{{portfolio.title}}">
    </a>

</div>


</body>
</html>

controllers.js File:

'use strict';

/* Controllers */

var portfolioApp = angular.module('portfolioApp', []);

portfolioApp.controller('PortfolioCtrl', ['$scope', '$http', function($scope, $http) {
$http.get('portfolio.json').success(function(data) {
$scope.portfolios = data;
});

}]);

portfolio.json File:

[
{
    "sideclass": "lsidebar", 
    "title": "Revolt Theory Design", 
    "url": "revolt_theory_vertical.jpg", 
    "urlthumb": "revolt_theory_vertical_tn.png"
}, 
{
    "sideclass": "rsidebar", 
    "title": "Macro Elements Business Card", 
    "url": "macro_elements_card.jpg", 
    "urlthumb": "macro_elements_card_tn.png"
}
]

Solution

  • I wasn't able to get it to work using $evalAsync, but was able to use $timeout:

    'use strict';
    
    /* Controllers */
    
    var portfolioApp = angular.module('portfolioApp', []);
    
    portfolioApp.controller('PortfolioCtrl', ['$scope', '$http', '$timeout', function($scope, $http, $timeout) {
    $http.get('portfolio.json').success(function(data) {
    $scope.portfolios = data;
    
    $timeout(function() {
        $(".portfolio-gallery").colorbox({rel:"portfolio-gallery", slideshow:true})
    }, 100);
    
    });
    }]);
    

    Please share if there's a better solution