Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angularjs jquery插件不断添加dom元素_Javascript_Jquery_Angularjs - Fatal编程技术网

Javascript Angularjs jquery插件不断添加dom元素

Javascript Angularjs jquery插件不断添加dom元素,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我在angular指令中使用jQuery插件(如下)。我遇到的问题是,它似乎总是在重新初始化插件并添加与插件相关的新dom元素,而不是重新使用现有的元素 这是我的指令: angular.module('directives').directive('dateSelector', [function () { 'use strict'; return { link: function ($scope, elm, attrs) { $scope.query = $sc

我在angular指令中使用jQuery插件(如下)。我遇到的问题是,它似乎总是在重新初始化插件并添加与插件相关的新dom元素,而不是重新使用现有的元素

这是我的指令:

angular.module('directives').directive('dateSelector', [function () {
  'use strict';

  return {
    link: function ($scope, elm, attrs) {

      $scope.query = $scope.query || {};
      $scope.query = {
          start: moment().subtract('days', 29).format('YYYY-MM-DD')
        , end: moment().format('YYYY-MM-DD')
      }
      elm.find('span').html(moment().subtract('days', 29).format('MMMM D, YYYY') + ' - ' + moment().format('MMMM D, YYYY'));

      elm.daterangepicker({
          ranges: {
            'Today': [moment(), moment()],
            'Yesterday': [moment().subtract('days', 1), moment().subtract('days', 1)],
            'Last 7 Days': [moment().subtract('days', 6), moment()],
            'Last 30 Days': [moment().subtract('days', 29), moment()],
            'This Month': [moment().startOf('month'), moment().endOf('month')],
            'Last Month': [moment().subtract('month', 1).startOf('month'), moment().subtract('month', 1).endOf('month')]
          },
          startDate: moment().subtract('days', 29),
          endDate: moment()
        },
        function(start, end) {
          elm.find('span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
          $scope.query = {
              start: start.format('YYYY-MM-DD')
            , end: end.format('YYYY-MM-DD')
          }
          if (angular.isFunction($scope.dateUpdate)) $scope.dateUpdate(start.format('YYYY-MM-DD'), end.format('YYYY-MM-DD'));
        });

    }

  };

}]);
有没有办法让它重复使用相同的
elm


谢谢

你是说你不想让插件添加…-保存日历和其他拾取元素的元素?你能为此创建一个plnkr吗?@j.wittwer,对-我认为它需要创建一次。但在这种情况下,当我更改路由时,它不会破坏旧路由,并创建新路由,因此我最终得到一堆