简体   繁体   English

Angular指令中的jQuery插件仅可使用一次

[英]Jquery plugin in Angular directive only works once

I am using this jquery plugin that allows to create a "pinterest" grid. 我正在使用此jquery插件,该插件可创建“ pinterest”网格。 I integrated the plugin in an Angular directive and it works fine when I load the webpage for the first time. 我将插件集成到Angular指令中,并且在我第一次加载网页时可以正常工作。 However, if I navigate to another path and go back to the grid, it doesn't work anymore(it doesn't compile as it should). 但是,如果我导航到另一条路径并返回到网格,它将不再起作用(它不会按应有的方式进行编译)。

Example: 例:

  • I load the application by going to where the grid is(/food) 我通过转到网格所在的位置来加载应用程序
  • Then I navigate to this path(/recipe) 然后我导航到该路径(/配方)
  • Then I go back to this path(/food) 然后我回到这条路

The directive gets call, but the plugins doesn't apply on the dom 该指令会被调用,但插件不适用于dom

Angular directive: Angular指令:

'use strict';

angular
    .module('app')
    .directive('foodDirective', foodDirective);

foodDirective.$inject= ['$timeout','$compile'];

function foodDirective($timeout,$compile) {

    return {

    restrict: 'A',
    link: function(scope, element, attrs) {

                element.pinterest_grid({

                        no_columns: 4,
                        padding_x: 10,
                        padding_y: 10,
                        margin_bottom: 50,
                        single_column_breakpoint: 700

                    });;
    }
};
}

Html HTML

<section food-direction><!--some html--></section>

Try modifying your "link" function as so: 尝试这样修改“链接”功能:

function(scope, element, attrs) {

  element.pinterest_grid({
    no_columns: 4,
    padding_x: 10,
    padding_y: 10,
    margin_bottom: 50,
    single_column_breakpoint: 700
  });

  scope.$on('$destroy', function(){
    // remove all event handlers that were
    // bound by the pinterest_grid plugin
    // (plugin "destroy" method if there is one)
  });
}

Your issue may be double/multiple bindings of the same event handlers, which can cause undesired behavior. 您的问题可能是同一事件处理程序的双重/多重绑定,这可能会导致不良行为。

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

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