I'm trying to integrate jQuery MixItUp in an angular.js project. For this purpose i'm using the following custom directive.
app.directive('mixitup', function($compile) {
return {
restrict: 'A',
scope: {
entities: '='
},
link: function(scope, element, attrs) {
scope.$watchCollection('entities', function() {
angular.element(element).mixItUp({
animation: {
duration: 200
},
load: {
sort: 'date:desc'
},
debug: {
enable: true,
mode: 'verbose'
}
});
}, true);
}
};
});
The directive is used in HTML as following.
<div mixitup="mixitup" id="mixitup-container" entities="medias">
<div ng-repeat="media in medias"
data-date="{{ media.date }}"
class="{{ itemClass }} {{ media.category }}">
<div class="image">
<img ng-src="{{ media.cover_image }}" class="img-responsive"></div>
</div>
</div>
The medias
collection is filled in a controller by fetching JSON data from custom services to connect the angular app to a Laravel 5.1 app. The success callback calls the following function. When logging the collection in the directive, it looks good.
$scope.addMedias = function addMedias(data) {
for (var i=0; i<data.length; i++) {
$scope.medias.push(data[i]);
}
$scope.loading = false;
};
The content is loaded as normal, but the MixItUp-Tiles stay hidden with display: none
. When adding filters to the view, the tiles show up when filtering, but not at startup.
I also tried removing floats from Twitter Bootstrap 3 in CSS, because MixItUp is using display: inline-block
.
#mixitup-container .mix {
display: none;
float: none;
}
Also the MixItUp debug script doesn't show any errors. But #mixitup-container
gets class fail
attached.
I managed to integrate jQuery MixItUp into my AngularJs app.
The custom directive now looks like this:
I used angular broadcast functionality to init MixItUp once all data is loaded. This is done by triggering the following:
The view HTML looks like this:
I also included the prefered css rule in my less:
One small problem remains. When i visit the page with MixItUp the first time, everything is okay, but the second time, the filters and sorting aren't working anymore. I'm using the angular
$routeProvider
to manage routing. There is another question relating this issue here: How to initiate MixItUp with AngularJS NgRoute