简体   繁体   English

将jQuery MixItUp集成到angular.js项目中

[英]Integrating jQuery MixItUp in an angular.js project

I'm trying to integrate jQuery MixItUp in an angular.js project. 我正在尝试将jQuery MixItUp集成到angular.js项目中。 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. 该指令在HTML中的用法如下。

<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. 通过从自定义服务中获取JSON数据以将angular应用程序连接到Laravel 5.1应用程序,将medias集合填充到控制器中。 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 . 内容正常加载,但MixItUp-Tiles保持隐藏状态, 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 . 我还尝试了从CSS中的Twitter Bootstrap 3中删除浮点数,因为MixItUp使用display: inline-block

#mixitup-container .mix {
    display: none;
    float: none;
}

Also the MixItUp debug script doesn't show any errors. 而且,MixItUp调试脚本不会显示任何错误。 But #mixitup-container gets class fail attached. 但是#mixitup-container获取附加的类fail

I managed to integrate jQuery MixItUp into my AngularJs app. 我设法将jQuery MixItUp集成到我的AngularJs应用程序中。
The custom directive now looks like this: 现在,自定义指令如下所示:

myApp
.directive('mixitup', function($compile) {

    return {
        restrict: 'A',
        scope: {
            datasource: '=',
            add: '&',
        },
        link: function(scope, element, attrs) {

            scope.$on('init-mixitup', function(event) {
                console.log('init');
                angular.element(element).mixItUp({
                    animation: {
                        duration: 200
                    },
                    load: {
                        sort: 'myorder:desc'
                    }
                });
            });

            scope.$on('resort-mixitup', function(event, sortCommand) {
                angular.element(element).mixItUp('sort', sortCommand);
            });
        }
    };
});

I used angular broadcast functionality to init MixItUp once all data is loaded. 一旦所有数据加载完毕,我就使用了角度广播功能来初始化MixItUp。 This is done by triggering the following: 通过触发以下操作来完成此操作:

// init
$rootScope.$broadcast('init-mixitup');

// sort
$rootScope.$broadcast('resort-mixitup', 'myorder:desc');

The view HTML looks like this: 视图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>

I also included the prefered css rule in my less: 我也将首选的CSS规则包括在我的以下代码中:

#mixitup-container {
    .mix {
        display: none;
    }
}

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. 第一次使用MixItUp访问该页面时,一切正常,但是第二次,过滤器和排序不再起作用。 I'm using the angular $routeProvider to manage routing. 我正在使用有角的$routeProvider来管理路由。 There is another question relating this issue here: How to initiate MixItUp with AngularJS NgRoute 还有一个与此问题相关的问题: 如何使用AngularJS NgRoute初始化MixItUp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM