我正在使用带有键和值的请求标头来从AngularJS控制器读取。如果该标头(在这种情况下)AppHeaders
有效且具有自定义值,则需要从该控制器触发单击。我有的代码,如果是这样的话:
$scope.$applyAsync(function() {
if (appHeaders != null && appHeaders['header-name'] != null) {
if (appHeaders['header-name'] == "custom-value") {
$('.class-name').click();
}
}
});
怎么了?我对此进行了深入调试,条件工作正常。我想这个问题是因为点击被激活时DOM上的元素不存在。
答案 0 :(得分:0)
感谢您的帮助!最终的解决方案结果是在声明的函数上应用$ 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();
}
});
我写了一篇文章,整篇研究解释了这一点: https://jbrizio.github.io/2017/10/20/Trigger-click-when-determinate-requests-finish-using-AngularJS.html