【问题标题】:respond to jquery event within an angular digest in an angular component响应角度组件中的角度摘要中的 jquery 事件
【发布时间】:2018-01-13 18:08:18
【问题描述】:

我正在尝试在 angularJS 中的 $onInit 函数上进行这项工作。

popup() {
    this.$log.log("popup clicked...");
}

constructor(
    public $log: ILogService,
    public $timeout: ITimeoutService,
    public $q: IQService,
    public $element: any) {

    // i have tried this
    $element.on("click custom:event", $q.when(() => this.popup()));

    // and this.
    $element.on("click custom:event", $timeout(() => this.popup(), 1000));

    // I have also tried the scope (last resort)
    $element.on("click custom:event", $scope.apply(() => this.popup());
}

在每种情况下,错误都是:

Uncaught TypeError: ((jQuery.event.special[handleObj.origType] || (intermediate value)).handle || handleObj.handler).apply 不是一个函数 在 HTMLElement.dispatch 在 HTMLElement.elemData.handle

有没有办法正确地创建一个承诺,以便在角度摘要中触发 jQuery 事件?

【问题讨论】:

    标签: javascript angularjs typescript angular-promise


    【解决方案1】:

    对于任何可能受益的人......我是愚蠢的。

        this.$element.on("click", () => $q(() => this.popup()));
    

    【讨论】:

    • 这里$q的选择是随机的。它创造了一个永远不会被使用的承诺。它应该是 $scope.apply 。
    • 之所以有效,是因为 $q 创建了一个摘要,但是这里 $q 的用法是错误的。有一个错字。这是 $scope.$apply,而不是 $scope.apply。
    猜你喜欢
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    相关资源
    最近更新 更多