来自 AngularJS 控制器的触发点击

Trigger Click from AngularJS Controller

我正在使用请求 header,其中包含要从 AngularJS 控制器读取的键和值。如果 header 在本例中是 AppHeaders 有效且具有自定义值,我需要从该控制器触发点击。我拥有的代码,如果是这样的话:

$scope.$applyAsync(function() {
    if (appHeaders != null && appHeaders['header-name'] != null) {
        if (appHeaders['header-name'] == "custom-value") {
            $('.class-name').click();
        }
    }
});

怎么了?我对此进行了深入调试,条件工作正常。我猜问题是因为在触发点击时 DOM 上的元素不存在。

感谢你们的帮助!最终解决方案是在声明的函数上应用 $broadcast 事件,并使用计数器来验证调用的最后一个周期以触发对元素的点击。

// Here we declare an empty array to store each request
var requests = [];

// Our first function
$scope.firstFunction = function() {
    // Execute broadcast event with the name of the function
    $scope.$broadcast('requestEnded',requests.push('firstFunction'));
};

// Our last function
$scope.secondFunction = function() {
    // Execute broadcast event with the name of the function
    $scope.$broadcast('requestEnded', requests.push('secondFunction'));
};

// This listener is executed each time that requestEnded is fired
$scope.$on('requestEnded', function(event, countRequests) {
    // Here we validate that the count of requests is the desire
    if (countRequests == 2) {
        // Trigger Click
        $('.class-selector').click();
    }
});

我写了一个 post 整个研究解释了这一点: https://jbrizio.github.io/2017/10/20/Trigger-click-when-determinate-requests-finish-using-AngularJS.html