gpt4 book ai didi

javascript - Angular : is it possible to pass an array or multiple strings to the $on event listener so as not to have multiple lines of listeners?

转载 作者:行者123 更新时间:2023-12-02 14:53:02 27 4
gpt4 key购买 nike

我有类似的东西:

$scope.$on(config.SOME_CONSTANT, ()=> {
activate();
// plus a bunch of instantiations
$scope.$digest();
});

$scope.$on(config.SOME_OTHER_CONSTANT, ()=> {
activate();
$scope.$digest();
});

是否可以将它们合并为 1 个听众,以便更加干燥? Angular 文档说它只需要一个字符串参数...我有一些条件性的东西,我希望在事件发生的两种情况下都发生。有什么想法吗?

最佳答案

您可以通过循环来实现它:) 工作 JsFiddle Link

angular.forEach(['one', 'two', 'three'], function(value) {
$scope.$on(value, function(event) {
console.log(event.name);
});
});

关于javascript - Angular : is it possible to pass an array or multiple strings to the $on event listener so as not to have multiple lines of listeners?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36024787/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com