【问题标题】:link function assigned to scope is not accessible inside controller constructor function分配给范围的链接函数在控制器构造函数中不可访问
【发布时间】:2020-01-03 11:36:21
【问题描述】:

我有一个控制器构造函数。 还有一些 dom 操作的指令。

当我们从控制器调用链接函数deleteNotification时,它给出错误

deleteNotification 不是函数。

我们如何在控制器中获取链接函数的范围和访问方法deleteNotification。

任何帮助将不胜感激。

我们如何在控制器中获取链接函数的范围和访问方法deleteNotification。

export class triggeredNotificationController{
    constructor($scope){
       this.$scope = $scope
       this.deleteNotification();
    }
    deleteNotification(){
       self.$scope.deleteNotification();
    }
}

triggeredNotificationController.$inject=['$scope'];

cModule.controller('triggeredNotificationController',triggeredNotificationController)

cModuleAlertsModule.directive('deleteNotification',[
    function(){
        return {
            scope: false,
            controller: triggeredNotificationController,
            link: function(scope, element, attrs, ctrl){
                scope.deleteNotification=  function(selectedRowData){
                    //some dom manipulation
                }
            }
        }
    }
])
export default cvModuleAlertsModule;

【问题讨论】:

  • 链接在控制器之后执行。你可以使用 $timeout 或 $scope.$watch 。
  • 如果你不需要使用$compile,你就不需要link,如果你的模板写得很好。
  • 由于某种原因我不得不使用$compile,现在需要使用$timeout..请您解释一下吗

标签: javascript angularjs angularjs-directive


【解决方案1】:

可以使用$postLink生命周期钩子来延迟函数的调用:

export class triggeredNotificationController{
    constructor($scope){
       this.$scope = $scope
    }
    $postLink(){
       this.$scope.deleteNotification();
    }
}

有关详细信息,请参阅

【讨论】:

    猜你喜欢
    • 2013-05-06
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    相关资源
    最近更新 更多