I integrated jQuery MixItUp with AngularJS NgRoute with the use of a custom directive.
I use the AngularJS $broadcast
and $on
functions to handle communication between Controller and Directive:
myApp
.directive('mixitup', function($compile) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
scope.$on('init-mixitup', function(event) {
// console.log('[event] înit-mixitup');
angular.element(element).mixItUp({
animation: {
duration: 200
},
load: {
sort: 'myorder:desc'
}
});
});
scope.$on('resort-mixitup', function(event, sortCommand) {
// console.log('[event] resort-mixitup');
angular.element(element).mixItUp('sort', sortCommand);
});
scope.$on('destroy-mixitup', function(event) {
// console.log('[event] destroy-mixitup');
angular.element(element).mixItUp('destroy');
})
}
};
});
My view HTML:
<div class="btn-group controls">
<button class="btn btn-lg filter"
data-filter="all">All</button>
<button class="btn btn-lg filter"
data-filter=".photo">Photo</button>
<button class="btn btn-lg filter"
data-filter=".audio">Audio</button>
<button class="btn btn-lg filter"
data-filter=".video">Video</button>
</div>
<div mixItUp="mixItUp" id="mixitup-container">
<div ng-repeat="item in items"
id="{{ item.id }}"
style="display: inline-block;"
data-myorder="{{ item.date }}"
class="mix col-xs-6 col-sm-4 {{ item.category }}">
<img ng-src="{{ item.image }}" class="img-responsive img-circle">
</div>
</div>
In my controller handle jQuery MixItUp with the following calls:
// instantiate jQuery MixItUp
$rootScope.$broadcast('init-mixitup');
// sort jQuery MixItUp
$rootScope.$broadcast('resort-mixitup', 'myorder:desc');
You have to destroy jQuery MixItUp when leaving page. I managed this by adding the following to my controller:
$scope.$on("$destroy", function(){
$rootScope.$broadcast('destroy-mixitup');
});
You can also have a look at a very similar question i posted myself: jQuery MixItUp with AngularJS NgRoute