【问题标题】:Angular Directives return with a timeout attached?Angular 指令返回并附加超时?
【发布时间】:2014-01-03 00:01:50
【问题描述】:

所以我在我的网站中使用了一个 jQuery 插件的footables。我将它包装在一个角度指令中,如下所示:

angular.module('myApp').directive("footable", function() {
    return {
    restrict: 'A',
    link: function(scope, element, attrs){
        return element.footable(scope.$eval(attrs.footable));
    }
  }
});

然后我称之为footable

<table class="footable" footable="{breakpoints: {'phone': '639','tablet':'767'}}">

问题是,即使指令正在创建一个footable,footable 需要 100 毫秒才能工作,如果我在移动设备上启动它不会重排,除非我在控制器中发送超时,如下所示:

$timeout(function(){
            $('.footable').trigger('footable_redraw'); //force a redraw
    }, 100);

这个问题是我正在使用 jquery 并把它作为我的页面控制器。我想在指令中设置超时并重绘并让它工作吗?还是有其他方法可以让 finnicky jquery 插件工作?

【问题讨论】:

  • 很确定链接函数中的return 在这里没有做任何事情。据我所知,AngularJS compile 期望链接上没有返回对象。

标签: jquery angularjs timeout angularjs-directive footable


【解决方案1】:

您总是可以将超时移到指令中,对吗?

angular.module('myApp').directive("footable", function($timeout) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs){
            element.footable(scope.$eval(attrs.footable));
            $timeout(function(){
               element.trigger('footable_redraw');
            }, 100);
        }
    }
});

这是一个更好的Angular zen,但可能有更好的方法来处理footable本身。

【讨论】:

  • 谢谢!我现在可以在我的控制器中删除超时并且这有效。正是我需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
相关资源
最近更新 更多