Javascript Emit在$ rootScope之前调用。$ on已准备就绪

时间:2021-08-21 13:36:17

I'm having an issue with JavaScript. I've got an application which uses an EMIT function on load. But unfortunately the class which implements the $rootScope.$on is probably not ready yet. My workaround was to use a timeout but this solution is not ideal. Is there another solution? Thanks!

我遇到了JavaScript问题。我有一个在加载时使用EMIT功能的应用程序。但不幸的是,实现$ rootScope。$ on的类可能还没准备好。我的解决方法是使用超时但这个解决方案并不理想。还有其他解决方案吗?谢谢!

This function:

$rootScope.$on('newPrintSelected', function (event, selectedPrinter) {
    StorageModule.storage.find(collectionName, {_id : selectedPrinter.printer_hardware._id}, function(error, data){
        if (error) { //Not Found
            console.error("Ops. There is something wrong with this action...");
        }else {
                if(data[0].printOptions == null){
                    $scope.options = angular.copy($scope.default);
                    $scope.options.layerThickness = 30 - ($scope.options.layerThickness * 100);
                    $scope.persistedOptions = angular.copy($scope.default);
                    $scope.persistedOptions.layerThickness = 30 - ($scope.persistedOptions.layerThickness * 100);
                }else{
                    data[0].printOptions.layerThickness = 30 - (data[0].printOptions.layerThickness * 100);
                    $scope.options = angular.copy(data[0].printOptions);
                    $scope.persistedOptions = angular.copy(data[0].printOptions);
                }
            }
            $scope.selectedPrinterHardware = angular.copy(data[0]);
            console.warn($scope.options);
            $scope.$apply();
        });
});

Is not beeing called by this one:

不是这个叫做的:

$scope.setPrinter = function (printer, callback) {
        setTimeout(function(){ $rootScope.$emit('newPrintSelected', printer); }, 200);
    }
}

At the start up of the application I had to put the setTimeout in order to this work.

在启动应用程序时,我必须将setTimeout放到这项工作中。

1 个解决方案

#1


2  

Can you not re-architect your code to use a service which returns the value you would send with emit? (or a promise that will fulfill with the required value).

您是否可以重新设计代码以使用返回您将使用emit发送的值的服务? (或履行所需价值的承诺)。

If there is absolutely no way you can change the code generating the emit (and I really can't think of any reason why this would be) then you could store the value and listen for an event, say 'loaded', and then $emit at that point.

如果绝对没有办法你可以改变生成发射的代码(我真的想不出有什么原因)那么你可以存储值并监听一个事件,比如'加载',然后是$在那一点发射。

#1


2  

Can you not re-architect your code to use a service which returns the value you would send with emit? (or a promise that will fulfill with the required value).

您是否可以重新设计代码以使用返回您将使用emit发送的值的服务? (或履行所需价值的承诺)。

If there is absolutely no way you can change the code generating the emit (and I really can't think of any reason why this would be) then you could store the value and listen for an event, say 'loaded', and then $emit at that point.

如果绝对没有办法你可以改变生成发射的代码(我真的想不出有什么原因)那么你可以存储值并监听一个事件,比如'加载',然后是$在那一点发射。